This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
What is reflection
Reflection refers to the ability to obtain information about objects dynamically while a program is running. Is to import the module as a string; Through the form of a string, go to the module to find the specified function, and execute. Using strings to manipulate members in objects, a string based driver.
Built-in functions in Python reflection
getattr()
Getattr () : used to get the object of property, but it can’t get private variables, that is, the front with __ properties or methods, getattr (object, the name, the default), it has three parameters
- Object: the object
- Name: property value in the object
- Default: Optional return value if the property does not exist in the object.
Example:
class B:
def __init__(self) :
self.name = 'Bb'
self.age = 40
def add(self) :
print('B')
if __name__ == '__main__':
b = B()
print(getattr(b, 'name'.'not found')) # Output: Bb
print(getattr(b, 'cc'.'not found')) # output :not found
func = getattr(b,'add'.None)
print(func)
>
func() Output: # B
Copy the code
hasattr()
* * hasattr (object, name) * * this function is used to detect objects (object) contained in the name (does not mean that in the name refers to a property name) this property, in which the object is an object, the name refers to the need to check whether there is the name of the attribute of the object.
Example:
class B:
def __init__(self) :
self.name = 'Bb'
self.age = 40
def add(self) :
print('B')
if __name__ == '__main__':
b = B()
print(hasattr(b, 'ccc')) # False
print(hasattr(b, 'add')) # True
Copy the code
setattr()
Setattr (object,name,value) is to seta specified value for a property of an object, object is the object,name is the name of the property in the object, and value is the value you want to set. Three of the parameters are required.
This function adds new properties to an object and changes methods in the object.
Example:
class B:
def __init__(self) :
self.name = 'Bb'
self.age = 40
def add(self) :
print('B')
if __name__ == '__main__':
b = B()
print(b.age) # 40
setattr(b,'age'.50)
print(b.age) # 50
def cc() :
print('i am changed')
b.add() # B
setattr(b,'add',cc)
b.add() # i am changed
# print(dir(b))
Copy the code
delattr()
Delattr (object,name) deletes the specified properties of an object. Object is the object and name is the property name of the object.
Example:
class B:
def __init__(self) :
self.name = 'Bb'
self.age = 40
def add(self) :
print('B')
if __name__ == '__main__':
b = B()
print(dir(b)) # [,'add', 'age', 'name'] dir() ¶
print(hasattr(b,'age')) # True
delattr(b,'age')
print(dir(b)) # [,'add', 'name']
print(hasattr(b,'age')) # False
Copy the code
Dir () is used without variables to get a list of variables, methods, and types defined in the current scope. Returns a list of properties and methods with arguments.
Dir (Object) Object: object, variable, and type
Those are the four built-in functions in Python reflection.
The application of Python reflection
One application of reflection is url routing in the Web framework. The user can enter the URL route to locate the function in the views. The main application is getattr(), which is used to get the corresponding function in the views, and if not, return the value you set to determine whether to proceed.