Python special member methods for reflection and classes, python reflection Member
Reflection
Reflection refers to four built-in functions: getattr, hasattr, setattr, and delattr to obtain members, check members, set members, and delete members.
Class Dog (object): def _ init _ (self, name): self. name = name def eat (self): print ("% s is eating... "% self. name) def run (): print ("runing .... ") d = Dog (" lucy ") choise = input (" Enter the method to call: ") if hasattr (d, choise ): # determine whether an object has a corresponding string method func = getattr (d, choise) # obtain the memory address object func () else of the corresponding method or attribute in the object based on the string: setattr (d, choise, run) # setattr (obj, y, fun) is equivalent to obj. y = fun, fun can be an attribute or method v = getattr (d, choise) print (v)
Dir ([obj]):
Calling this method will return a list containing most of the obj attribute names (some special attributes are not included ). The default value of obj is the current module object.
Hasattr (obj, attr ):
This method is used to check whether obj has an attribute named attr and returns a Boolean value.
Getattr (obj, attr ):
Calling this method will return the attribute value named attr in obj. For example, if attr is 'bar', obj. bar is returned.
Setattr (obj, attr, val ):
To call this method, the attribute named attr of obj is assigned val. For example, if attr is 'bar', it is equivalent to obj. bar = val.
_ Doc _ view the description of the tip
_ Module _ indicates the module where the current operation object is located
_ Class _ indicates the class to which the object of the current operation belongs.
_ Init _ constructor creates an object through the class and runs it automatically.
_ Del _ destructor. The current object is released in the memory and executed automatically.
_ Call _ an object is followed by parentheses to trigger execution
_ Dict _ view members in a class or object
_ Str _ if this method is defined in a class, the return value of this method is output when such an object is printed.
_ Getitem _ when a dictionary attribute member is defined in the class, you can obtain
_ Setitem _ set to modify Dictionary data in the class
_ Delitem _ Delete Dictionary data in the class
_ Metalass _ indicates who will instantiate and create the class.
_ New _ trigger _ init _ create an instance
From lib. ss import a # EXAMPLE Class dogclass doges (object): "Class description" def _ init _ (self, name, food): self. name = name self. food = food self. data ={}# define the dictionary def _ call _ of A Class (self, * args, ** kwargs): # execute print (* args) with parentheses after the object) def _ str _ (self): # return self. name def _ getitem _ (self): # return self. data def _ setitem _ (self, key, value): # You can set the dictionary self of the class. data [key] = value def _ delitem _ (self, key): # You can delete the dictionary content of the class del self. data [key] dog = doges ('xxx', 'II') print (dog. _ doc _) B = a () print (B. _ module _) # print (dog. _ class _) # What is the class of the object in the current operation dog ('000000') # print (doges. _ dict _) # view the member classes of a class or object. Only print the members of a class. _ dict _) # view the Member objects of a class or object. print only the members of the object and not the members of the class. print (dog) # print the return value of _ str _ print (dog. _ str _ () # print the returned value dog ['1'] = 1000 # Trigger. _ setitem _ () dog ['2'] = 1000 # Trigger. _ setitem _ () print (dog. _ getitem _ () print (dog. _ delitem _ ('1') # Delete the dictionary print (dog. _ getitem _ () # Set the special method def func (self) of the class: print ('Hello word % s' % self. name) print () def _ init _ (self, name, age): self. name = name self. age = age ## type parameter 1: class name 2. class base class 3. class member, Dictionary format CAT = type ('cat', (object,), {'func': func, '_ init _' :__ init __}) cat = CAT ('miaomiao ', 3) cat. func () print (cat. name, cat. age)