The GetAttr () function is the core function of Python introspection, which is used in the following general context:
Get Object reference GetAttr
GetAttr is used to return an object property, or a method
- class A:
- def __init__ (self):
- self.name = ' zhangjing '
- #self. age= ' 24 '
- def method (self):
- Print"method print"
- Instance = A ()
- Print getattr (Instance, ' name, ' not find ') #如果Instance object that has the attribute name prints the value of self.name, otherwise printing ' Not find '
- Print getattr (Instance, ' age ', ' not find ') #如果Instance object with attribute age prints the value of self.age, otherwise printing ' not ' Find '
- Print getattr (A, ' method ', ' default ')
- #如果有方法method, otherwise print its address, otherwise print default
- Print getattr (A, ' method ', ' Default ') ()
- #如果有方法method, run the function and print none otherwise print default
Note: Factory mode can be easily implemented with GetAttr.
Example: A module supports printing in HTML, text, XML and other formats, and calls different functions to implement output in several formats, depending on the formate parameter passed in.
- 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 arguments
is an object, a string and an arbitrary value. The string may name an existing
attribute or a new attribute. The function assigns the value to the attribute,
Provided the object allows it. For example, was setattr(x,
‘foobar‘, 123)
equivalent to
x.foobar = 123
.
This is the relative getattr (). A parameter is an object, a string, and an arbitrary value. The string may list an existing property or a new property. This function assigns the value to the property. The object allows it to be provided. For example, SetAttr (x, "Foobar", 123) is equivalent to X.foobar = 123.
This is a relative of setattr (). The arguments is
An object and a string. The string must be the name of the one of the object ' s
Attributes. The function deletes the named attribute, provided the object allows
It. For example, is delattr(x, ‘foobar‘)
Equivalent to del x.foobar
.
A set of functions related to SetAttr (). Parameters are made up of an object (remember that everything in Python is an object) and a string. The string argument must be one of the object property names. The function deletes a property specified by string for the obj.delattr(x, ‘foobar‘)=
del x.foobar
Hasattr is used to determine whether an object has a property.
Syntax: Hasattr (object, name), BOOL determines whether the object has a Name property 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")
Python's GetAttr (), SetAttr (), delattr (), hasattr ()