Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to use __getattribute__(self,name) and __setattr__(self,name,val)
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