Analysis of getattr (), setattr (), delattr (), hasattr (), and getattrsetattr in Python
Getattr () functionIs the core function of Python introspection. The usage is as follows:
Get object reference getattr
Getattr is used to return an object attribute or a method.
Class A: def _ init _ (self): self. name = 'hangjing' # self. age = ''def method (self): print "method print" Instance = A () print getattr (Instance, 'name, 'not find ') # print self if the Instance object has a property name. name value. Otherwise, print 'not found 'print getattr (Instance, 'age', 'not found') # If the Instance object has an attribute age, print self. age value. Otherwise, print 'not find 'print getattr (a, 'method', 'default') # if there is a method, otherwise print its address, otherwise, print the default print getattr (a, 'method', 'default') () # if there is a method, run the function and print None. Otherwise, print the default
Note: You can use getattr to easily implement the factory mode.
For example, a module supports printing in html, text, xml, and other formats. Based on the input formate parameter, different functions are called to implement output in several formats.
import statsout def output(data, format="text"): output_function = getattr(statsout, "output_%s" % format) return output_function(data) setattr(object, name, value)This is the counterpart of getattr(). The argumentsare an object, a string and an arbitrary value. The string may name an existingattribute or a new attribute. The function assigns the value to the attribute,provided the object allows it. For example, setattr(x,'foobar', 123) is equivalent tox.foobar = 123.
This is the corresponding getattr (). A parameter is an object, a string, and any value. A string may list an existing attribute or a new attribute. This function assigns the value to the attribute. This object allows it to provide. For example, setattr (x, "foobar", 123) is equivalent to x. foobar = 123.
Delattr (object, name)
This is a relative of setattr (). The arguments are
An object and a string. The string must be the name of one of the object's
Attributes. The function deletes the named attribute, provided the object allows
It. For example, delattr (x, 'foobar') is
Equivalent to del x. foobar.
A group of functions related to setattr. A parameter is composed of an object (remember that everything in python is an object) and a string. The string parameter must be one of the Object Property names. This function deletes an attribute specified by string for this obj. Delattr (x, 'foobar') = del x. foobar
• Hasattr is used to determine whether an object has an attribute.
Syntax:
Hasattr (object, name)-> bool
Checks whether the object has the name attribute and returns a Boolean value.
>>> li=["zhangjing","zhangwei"]>>> getattr(li,"pop")<built-in method pop of list object at 0x011DF6C0>>>> li.pop<built-in method pop of list object at 0x011DF6C0>>>> li.pop()'zhangwei'>>> getattr(li,"pop")()'zhangjing'>>>getattr(li, "append")("Moe")