Reflection:
What is reflection:
The main point is the ability of a program to access, detect, and modify its own state or behavior.
All things in Python are objects.
Four functions for self-reflection:
Both classes and objects can be used.
classA:name='ABC' Age= 19#use of the classPrint(Hasattr (A,'N1me'))#determines whether a class contains an attribute hasattr (class or object name, property name)Print(GetAttr (A,'name'))#gets the property value of the corresponding property GetAttr (class or object name, property name, content returned by error) <----error returns if you do not write, when you get a non-existent property will be an error, write will return the content of the writeSetAttr (A,'name','AAA')#sets the property value of a property SetAttr (class or object name, property name, property value)Print(A.name)PrintA.__dict__) delattr (A,'name')#Delete attribute delattr (class or object name, property name)PrintA.__dict__)#use of ObjectsP1 = A ()#instantiating objects, using the same method as abovePrint(Hasattr (P1,' Age'))Print(GetAttr (P1,' Age')) SetAttr (P1,' Age', 20)Print(P1.age) delattr (P1,' Age')Print(P1.__dict__) Execution Result: D:\Python\Python36-32\python.exe e:/python/day-22/practice. pyfalseabcaaa{'__module__':'__main__','name':'AAA',' Age': 19,'__dict__': <attribute'__dict__'Of'A'Objects>,'__weakref__': <attribute'__weakref__'Of'A'Objects>,'__doc__': None} {'__module__':'__main__',' Age': 19,'__dict__': <attribute'__dict__'Of'A'Objects>,'__weakref__': <attribute'__weakref__'Of'A'Objects>,'__doc__': None} True1920{}process finished with exit code 0
Additional Command parameters:
__STR__: Changes the object's string display.
The original is to define a function in the class to display information, and each call requires the Object + method call to use, we can print this object directly after using __STR__ can display information.
classpeolple:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=Sexdef __str__(self):return 'name%s\nage:%s\nsex:%s'%(self.name,self.age,self.sex) #定义信息返回格式a= Peolple ('ABC', 18,'male')Print(a) Results of implementation: D:\PYTHON\PYTHON36-32\python.exe e:/python/day-22/practice. Pynameabcage:18Sex:maleprocess finished with exit code 0
__DEL__: Delete
Cleanup objects are used, and execution is triggered when the object finishes or is cleaned.
classpeolple:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=Sexdef __str__(self):return 'name:%s\nage:%s\nsex:%s'%(Self.name,self.age,self.sex)def __del__(self):Print('%s is del'%self.name) A= Peolple ('ABC', 18,'male')dela #对象 A is deleted, triggering __del__Print('----------Main program--------------') Execution Result: D:\Python\Python36-32\python.exe e:/python/day-22/practice. Pyabc is del----------Main program--------------Process finished with exit code 0 change:classpeolple:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=Sexdef __str__(self):return 'name:%s\nage:%s\nsex:%s'%(Self.name,self.age,self.sex)def __del__(self):Print('%s is del'%self.name) A= Peolple ('ABC', 18,'male')Print('----------Main program--------------'#程序执行完毕后, the __del__ execution result is triggered when the A object is cleaned: D:\Python\Python36-32\python.exe e:/python/day-22/practice. PY----------Main program--------------ABC is delProcess finished with exit code 0
Access the corresponding properties using the form of brackets, such as a View dictionary:
Similar to: print (a[' name ')
classpeolple:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=Sexdef __getitem__(self, item): #查询属性returngetattr (self, item)def __setitem__(self, Key, value): #设置属性 setattr (Self,key,value)def __delitem__(self, key): #删除属性 delattr (Self,key) a= Peolple ('ABC', 18,'male') #实例化对象Print(a['name']) #获取name的属性值 a['name'] ='a' #设置name的属性值Print(a['name'])dela['name'] #删除name PropertiesPrint(A.__dict__) Execution Result: D:\Python\Python36-32\python.exe e:/python/day-22/practice. pyabca{' Age': 18,'Sex':'male'}process finished with exit code 0
Python base day-22[Object-oriented: reflection, other command parameters]