python reflective object-oriented: the object-related properties operated by a string. The python everything is an object (reflection can be used)
four forms: getattr setattr delattr the hasattr
getattr (obj, name)
Example: class reflection
# Attribute reflecting object # method reflecting object # properties of reflection class # method of reflection type: a classmethod staticmethod class the Person: __mongy = 1000 nationality = ' China ' DEF the __init__ (Self, name, Sex, height): the self.name = name self.sex = Sex Self. __height = height @Property DEF height (Self): return Self. __height @classmethod DEF Money (CLS): CLS.__mongy = 0 return ' RUN OUT of Money,% S ' % CLS. __mongy Aike = the Person ( ' Aike ' , ' man ' , ' 180 [ ' ) print (getattr (Aike, ' name ' )) # reflecting object properties print (getattr (Aike, ' height ' )) # method reflecting object print (getattr (Aike, ' Money ' ) ()) # reflecting object method print(getattr (the Person, ' Money ' ) ()) # reflected class methods Print (getattr (the Person, ' nationality ' )) # reflection properties of the class # printing: Aike 180 [ RUN OUT of Money, 0 RUN OUT of Money, 0 china
Example: reflection module
Suppose module func, reflected at the source
DEF Login (): # function Print ( ' successful login ' ) A = 100 # Attribute class Teacher: # Class School = ' Tsinghua ' # @classmethod # class object implement DEF info_func (CLS): # object method Print ( ' which is a dynamic attribute class ' ) Import sys # getattr (the sys.modules [ '__ main __'], 'Login') () # reflected in the source file of the module, for acquiring an object method requires the use of sys obtaining module getattr ( the sys.modules [ the __name__ ], ' Login ') () # If you need to perform at the reflection object invokes the module, it is necessary to '__main__' into __name__, # principle is that when we direct the implementation of this module, name__ __ == '__main__', # when we perform other modules, refer to this module in other modules when the module __name__ == 'module name' IF __name__ == ' __main__ ' : Print (sys.modules)
func module is called:
# Reflection module attributes # may reflect the built-in module # reflection module method # folder under the file is assumed module FUNC Import FUNC # func.login () function call module # getattr (FUNC, ' Login ' ) () # reflection function module # Print (func.a) # calling module attributes Print (getattr (FUNC, ' a ' )) # attribute reflection module Print (getattr (func.Teacher, ' School ' )) # reflection module attribute class Aike = getattr (FUNC, ' Teacher ' ) () # reflection obtained based Teacher, plus () is to create an object getattr (func.Teacher, ' info_func ' ) (Aike) # function parameters have to be reflective of how to do? pass a qualifying parameters can
hasattr: determining whether the object has a property or method returns Ture or False, in conjunction with the general and getattr
# Continue to be an example module func Import func Print (the hasattr (func, ' Login ' )) IF the hasattr (func, ' Login ' ): # If there longin func attributes, the reflected getattr (func, ' Login ' ) () # Print: True successful login
setattr: Setting Properties
# '> `XY = V - setattr (X,' Y ', V) class : A Pass A = A () setattr (A, ' name ' , ' Ike ' ) # attribute object setattr (A, ' name ' , ' Aike ' ) # class static properties Print (a.name) # Aike Print (a.name) # Ike
delattr: Delete property
class A: Pass A = A () setattr (A, ' name ' , ' Ike ' ) # attribute object setattr (A, ' name ' , ' Aike ' ) # class static properties Print (A.name) # Aike Print (a.name) # Ike delattr (A, ' name ' ) Print (a.name) # Ike delattr (A, ' name ' ) Print(a.name) # error
Note: Python in everything is an object, the object can be used to reflection, reflection source in a module in the module attributes, object name acquisition method described modules need to use the sys module: sys.modules [ '__ main__ ']
If desired reflection at executing the object invokes the module, it is necessary to '__main__' into __name__
The principle is when we direct the implementation of this module, __ name__ return value '__main__', when we perform additional module, this module is referenced in other modules, this module __name__ return value 'is called module name '