Skip to content

Instantly share code, notes, and snippets.

@sente
Created July 25, 2012 06:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sente/3174676 to your computer and use it in GitHub Desktop.
Save sente/3174676 to your computer and use it in GitHub Desktop.
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