Python _ setattr __, _ getattr __, _ delattr __,__ getattribute __, pythongetattr
References
_ Setattr _, _ getattr _ and _ delattr _ and _ getattribute _ can intercept access to object attributes;
>>> S = Something ()
>>> S. age = 3
Set 'age' = 3
>>> S. age
3
Note that s. age does not call _ getattr __becauseThe _ getattr _ method is called only when the property is not found..
Although compared with Property, the implementation advantages are complex (may be less efficient ?), However, it is flexible and powerful;
In addition, for the above Code, access to unbound properties will cause an exception
>>>S. name
Get 'name'
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in _ getattr __
KeyError: 'name'
Note: Because name is not bound to s, _ getattr _ is called. Access to the dictionary with a nonexistent key will cause a KeyError exception;
More seriously, hasattr's behavior has been changed.
>>>Hasattr (s, 'name ')
Get 'name'
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in _ getattr __
KeyError: 'name'
Note that,Prevent loop calls, So_ Dict __Used to replace common feature assignment operations. In addition,_ Getattribute __Intercept access to all attributes (in the new class), and intercept access to _ dict! When accessing the self-related attributes in _ getattribute _, use the superclass _ getattribute _ method (usingSuperFunction) is the only safe way;
>>> S = Something ()
>>> S. name
Get name
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in _ getattribute __
AttributeError: 'something' object has no attribute 'name'
>>> S. name = 3
>>> S. name
Get name
3
_ Getattribute _ is called every time, Whether or not the attribute is bound;
_ Getattribute _ No _ getattr _ exception:
>>> Hasattr (s, 'T ')
False
When using _ getattribute _, be careful because it intercepts all attribute access, including _ dict __.
Def _ getattribute _ (self, key ):
...
Return self. _ dict _ [key]
A loop call occurs. You can use the super object to call _ getattribute _ to avoid this;
_ Getattr _ in python
I hope you can use a simple example to help you with the python built-in method:
========== Test. py
Class a (object ):
Name = "Jim"
Def _ setattr _ (self, name, value ):
Self. _ dict _ [name] = value
Def _ getattr _ (self, name ):
Return name
B = ()
Print dir (B)
Print B. _ dict __
Print B. name
Print B. notexists
Print
B. name = "change"
Print dir (B)
Print B. _ dict __
Print B. name
Print B. notexists
============ Running result ==========
% Python test. py
['_ Class _', '_ delattr _', '_ dict _', '_ doc __', '_ getattr _', '_ getattribute _', '_ hash _', '_ init _', '_ module __', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr _', '_ setattr __', '_ str _', '_ weakref _', 'name']
{}
Jim
Notexists
['_ Class _', '_ delattr _', '_ dict _', '_ doc __', '_ getattr _', '_ getattribute _', '_ hash _', '_ init _', '_ module __', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr _', '_ setattr __', '_ str _', '_ weakref _', 'name']
{'Name': 'change '}
Change
Notexists
======================================
At the beginning, we can see that there was nothing empty in the _ dict _ object of B. When we used B. previously defined name attribute: "Jim"
When we use B. after name = "change", the built-in method _ setattr __is called by default. At this time, I did not directly operate on self. the name attribute is directly modified in self. _ dict _ ['name'] = 'change '. then I use B. obtained when the name is accessed ...... remaining full text>
Python _ getattr __
# Write an example. Let's take a look.
Class Tests (object ):
Def _ setattr _ (self, name, value ):
Self. _ dict _ [name] = value
Def _ getattr _ (self, name ):
Return self. _ dict _ [name]
If _ name _ = "_ main __":
T = Tests ()
T. name = "hehe"
T. _ setattr _ ("age", 100)
Print t
Print t. name
Print t. age
Print t. _ dict __
Print object. _ dict __