The properties of a Python object can be obtained by obj.__dict__, adding a delete element to it to enable the dynamic addition and deletion of Python object properties, but we should use more formal getattr and setattr for this kind of operation.
- g Etattr (
object ,
name [,
default )
-
Return The value of the named attribute of Object . name must is a string. The If the string is the name of the one of the object ' s attributes, and the result is the value of this attribute. For Example, getattr (x, ' Foobar ') is equivalent to X.foobar . If the named attribute does not exist, default is returned If provided, otherwise attributeerror is raised.
GetAttr can dynamically get the properties of an object, and default can specify the value returned if a property named name does not exist, if not specified, and if the property name is not in object, returns Attributeerror
1>>>classDemo (object):2...def __init__(self):3... Self.name ='Demo'4...defcount (self):5...returnLen (self.name)6 ...7>>> X =Demo ()8>>>X.count ()94Ten>>> GetAttr (X,'Count')() One4 A>>> GetAttr (X,'name') - 'Demo' ->>> GetAttr (X,'notexist') the Traceback (most recent): -File"<stdin>", Line 1,inch<module> -Attributeerror:'Demo'object has no attribute'notexist' ->>> GetAttr (X,'notexist','Default_value') + 'Default_value'
- SetAttr (
object,
name,
value)
-
This is the counterpart of GetAttr (). The arguments is an object, a string and an arbitrary value. The string may be name an existing attribute or a new attribute. The function assigns the value to the attribute and provided the object allows it. For example, setattr (x, ' Foobar ', 123) are equivalent to x.foobar = 123.
corresponding to GetAttr, SetAttr is used to dynamically set the properties of an object to construct a configuration class object that is very suitable
>>> Y = Demo ()'color'red')> >> y.color'red'
View Code
When executing the above function is a default method in the class to respond, return or set the properties in obj.__dict__, we can define our own such function, that is, __getattr__, __setattr__, the former is not found in obj.__dict__ will be called
-
- object. __getattr__ (
Self,
name )
-
Called when an attribute lookup have not found the attribute in the usual places (i.e. it's not a instance attribute n Or is it found in the class tree for self ). name is the attribute name. This method should return the (computed) attribute value or raise an attribut Eerror exception.
note that if the attribute is found through the normal mechanism, __getattr__ () is not called. (This was an intentional asymmetry between __getattr__ () and __setattr__ () .) This was done both for efficiency reasons and because otherwise __getattr__ ( would have no-to-access other attributes of the instance. Note that at least-instance variables, you can fake total control by not inserting any of the values in the instance Attribut e dictionary (but instead inserting them in another object). See the __getattribute__ () method below for a-a-to-actually get total Control in New-style classes.
-
- object. __setattr__ (
Self,
name,
value )
-
Called when a attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store, the value in the instance dictionary). name is the attribute name, and value is the value of assigned to it.
if __setattr__ () wants to assign to an instance attribute, it should not simply execute self.name = Span class= "Pre" >value -this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g, self.__dict__[name] = value . For New-style classes, rather than accessing the instance dictionary, it should call the base class method with the same n Ame, for example, object.__setattr__ (self, name, Span class= "pre" >value) .
- object. __delattr__ (
Self,
name )
-
Like __setattr__ () and for attribute deletion instead of assignment. This should only being implemented if del obj.name is meaningful for the object.
By implementing these methods in the class, you can return some of the ' computed ' (obtained) properties
1>>>classZ:2... access_cnt = {}3...def __getattr__(self, name):4.. CNT =self.access_cnt.get (name, 0)5...Print 'Access CNT:', CNT + 16... self.access_cnt[name] = cnt + 17 ...8>>> z =Z ()9>>>Z.helloTenAccess Cnt:1 One>>>Z.hello AAccess Cnt:2 ->>>Z.hello -Access Cnt:3 the>>>Z.world -Access Cnt:1 ->>>Z.world -Access Cnt:2
Python object properties with GetAttr & SetAttr