The text and pictures in this article come from the network, only for learning, exchange, do not have any commercial purposes, copyright belongs to the original author, if you have any questions, please contact us to deal with
The following article comes from Tencent Cloud, by Su, one of the universe
(Want to learn Python? Python Learning exchange group: 1039649593, to meet your needs, materials have been uploaded to the group file stream, you can download! There is also a huge amount of new 2020Python learning material.)
introspection
This is also a tough feature of Python.
Introspection is what a program written in an object-oriented language can know about the type of an object at run time. The short sentence is that the runtime can get the type of the object. Such as the type (), dir (), getattr (), hasattr (), isinstance ().
A = b = [1, 2, 3] {' a ', 1, 'b' : 2, 'c' : 3} c = True print type (a), type (b), type (c) # < type 'list' > < type 'dict' > < type > 'Boolean' print isinstance(a,list) # TrueCopy the code
reflection
Reflection mechanism is at run time, dynamically determine the type of the object, and can call object attributes, methods, import modules through the string, is a string based event driven
hasattr
def hasattr(*args, **kwargs): # real signature unknown
"""
Return whether the object has an attribute with the given name.
This is done by calling getattr(obj, name) and catching AttributeError.
"""
pass
Copy the code
As we know from the source code comments, it returns whether the object has an attribute with the specified name. And it does this by calling getattr and catching AttributeError. As with the property call above, we can use hasattr(a, “test”) to determine whether eval can be used in this way.
def has_attr(obj, name): try: eval("obj.%s()" % name) return True except AttributeError as e: Return False a = Base() if has_attr(a, "test"): eval("a.test()") #Copy the code
But this approach is flawed because test prints twice, because we called test() twice, which is not what we want. If you use Hasattr, this function will not be called once.
getattr()
Now that you have a function to determine whether a property exists, you have to have a function to get it.
def getattr(object, name, default=None): # known special case of getattr """ getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. """ passCopy the code
Get the name attribute of an object. Think of object.name. If the default parameter is provided, the default value will be returned if the attribute does not exist. Here’s the same example:
A = Base() if hasattr(a, "test"): func = getattr(a, "test"Copy the code
Hasattr does not call test, and getattr takes the function object and does not call it. Instead, we execute a.test() by actively executing func(), which is much more flexible than exec and eval.
setattr
Now that you have the judge and get properties, you also need to set the properties.
def setattr(x, y, v): # real signature unknown; restored from __doc__
"""
Sets the named attribute on the given object to the specified value.
setattr(x, 'y', v) is equivalent to ``x.y = v''
"""
pass
Copy the code