Today's main content
1. Object-oriented advanced features---reflection
2. Built-in functions and built-in method additions
1. What is reflection:
By using the built-in function getattr (), hasattr (), SetAttr (), delattr (), the operation of the string outside the class, the ability to put static variables with the same name within the class, such as object properties, cannot be taken to the value.
It can also be understood as:
Under normal circumstances, if you can get this variable, then if there is a string form of this variable, you can get this value with reflection.
Use variable names of string data types to access names in a namespace
Find a property and you can find the value of this property directly
Find a way to find the memory address of this method
Cases:
classA:role=' Person' deffunc (self): a='b' Print("*"*Self )Print(GetAttr (A,'role'))#reflection of static variablesPrint(GetAttr (A,'func'))#the method in the class takes the memory addressGetAttr (A,'func') (3)#reflection of methods in a class
* * class uses names in the class namespace
Example: The following example uses the object name to get
classA:role=' Person' def __init__(self): Self.money= 500deffunc (self):Print('*'*10) A= A ()#instantiate a as the object namePrint(A.func) GetAttr (A,'func')() Print(GetAttr (A,' Money'))
Objects use methods and properties that can be used by objects
Introduction to Built-in functions:
Hasattr () Determine if there is a name in the namespace
GetAttr () Gets the value corresponding to the name from the namespace
Module Meaning: The module is the name of the python where the class resides
Cases:
deflogin ():Print('Perform the login function')defRegister ():Print('perform the Register function')ImportSYSPrint(sys.modules['__main__'])#<module ' __main__ ' from ' f:/python-script-2/s11-day23/a.py ' >Func = input ('>>>')ifHasattr (sys.modules['__main__'],FUNC)#Check the input information, whether a.py hasGetAttr (sys.modules['__main__'],func) ()#Execute the function if you have one
Class uses the name in the class namespace
GetAttr (class name, ' name ')
Objects use methods and properties that can be used by objects
GetAttr (object name, ' name ')
Module uses the name in the module
Import Module
GetAttr (module name, ' name ')
Import OS getattr (OS,'rename') ('user','user_info')
GetAttr must be used in conjunction with HASATTR
SetAttr # Modify and New
DELATTR # Deleting an attribute
SetAttr
Cases:
classA:def __init__(self,name): Self.name=namedefWahaha (self):Print('Wahahahahaha') A= A ('Alex') A.namea.wahaha () GetAttr (A,'name')Print(A.__dict__) SetAttr (A,' Age', 18)#added a propertyPrint(A.__dict__) Conclusion: wahahahahaha{'name':'Alex'}{'name':'Alex',' Age': 18}
Delattr
Cases:
classA:def __init__(self,name): Self.name=namedefWahaha (self):Print('Wahahahahaha') A= A ('Alex') A.namea.wahaha () GetAttr (A,'name')Print(A.__dict__) SetAttr (A,' Age', 18)Print(A.__dict__) delattr (A,' Age')Print(A.__dict__) Conclusion: wahahahahaha{'name':'Alex'}{'name':'Alex',' Age': 18}{'name':'Alex'}
2. Built-in function, method supplement
Isinstance is similar to the type method, it can determine the data type, but isinstance can also detect whether there is an inheritance relationship
Cases:
class B:passclass A (b):pass= A ()print(isinstance (a,a)) Print (Isinstance (A, b)) # ability to detect inheritance relationships Print is A) Print is B) # type only pure judgment class # Judging an object and a class there is no blood relationship isinstance (a,a)
__HASH__: no use.
__LEN__: The length of the computed value result in the class, in the same number as Len ()
Python Base 23rd Day (reflection)