First, reflection
Manipulate object-related properties in the form of strings. (Use the variable name of the string data type to get the value of the variable)
All things in Python are objects (you can use reflection)
#hasattrdefHasattr (*args, **kwargs):#Real Signature Unknown """Return whether the object has a attribute with the given name. This is do by calling GetAttr (obj, name) and catching Attributeerror. """ Pass#GetAttrdefGetAttr (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 equiv Alent to x.y. When a default argument is given, it's returned when the attribute doesn ' t exist; Without it, an exception was raised in the case. """ Pass#SetAttrdefSetAttr (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#delattrdefDelattr (x, y):#real signature unknown; restored from __doc__ """deletes the named attribute from the given object. Delattr (x, ' Y ') is equivalent to ' del x.y '""" Pass
4 ways to reflect the source code
#variables in reflection classes: Static Properties, class methods, static methodsclassFoo:school='Oldboy'Country=' China'language='chiness'@classmethoddefClass_method (CLS):Print(cls.school) @staticmethoddefStatic_method ():Print('In Staticmethod') defWahaha (self):Print('Wahaha')Print(Foo.school)#OldboyPrint(Foo.country)# ChinaPrint(Foo.language)#chiness#Judgment ImplementationINP = input ('>>>')ifINP = ='School':Print(Foo.school)#OldboyelifINP = ='Country':Print(Foo.country)# ChinaelifINP = ='language':Print(Foo.language)#chiness#Reflection Implementation while1: InP= Input ('>>>')#Output School, print Oldboy country, print the China output language, print chiness Print(GetAttr (FOO,INP))#Analytic GetAttr MethodPrint(GetAttr (Foo,'School'))#OldboyFoo.class_method ()#OldboyPrint(GetAttr (Foo,'Class_method'))#<bound method Foo.class_method of <class ' __main__. Foo ' >>GetAttr (Foo,'Class_method')()#Oldboy equivalent: Foo.class_method ()GetAttr (Foo,'Static_method')()#In Staticmethod equivalent to: Foo.static_method ()GetAttr Instances
Python reflection, built-in functions (__str__, __repr__)