-
-
Save jredrejo/ed80d525e4f36fec6ec6 to your computer and use it in GitHub Desktop.
Example of how to use __getattribute__(self,name) and __setattr__(self,name,val)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo(object): | |
def __getattribute__(self, name): | |
print "getting attribute %s" % name | |
return object.__getattribute__(self, name) | |
def __setattr__(self, name, val): | |
print "setting attribute %s to %r" % (name, val) | |
return object.__setattr__(self, name, val) | |
""" | |
python -i class_attribute_test.py | |
>>> foo = Foo() | |
>>> foo.var = 100 | |
setting attribute var to 100 | |
>>> foo.name = 'stuart' | |
setting attribute name to 'stuart' | |
>>> bar = foo.var | |
getting attribute var | |
>>> foo.var = foo.name | |
getting attribute name | |
setting attribute var to 'stuart' | |
"""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment