The "1" getattr () function is Python
the core function of introspection"2" Python's
Introspection: Python can view functions or module information stored as objects in memory, as well as related operations. "3" Related functions: GetAttr (), SetAttr (), delattr (), hasattr () GetAttr related usages are described below: GetAttr is used to return an object property, or the method can be visually discerned by a demo:
classA:def __init__(self): SELF.A='a' defmethod (self):Print "Method Print"a=A ()PrintGetAttr (A,'a','default')#If you have attribute a, print a, otherwise print the defaultPrintGetAttr (A,'b','default')#If you have attribute B, print B, otherwise print the defaultPrintGetAttr (A,'Method','default') #If there is a method, print its address, otherwise print the defaultPrintGetAttr (A,'Method','default')() #If there is a method, run the function and print none otherwise print default
Output
Adefault<bound method A.method of <__main__. A instance at 0x02cb7968>>method Printnone
Python's GetAttr ()-June 7, 2017