Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HappyCodingRobot/798519deea1a98aab277cc050a49df6f to your computer and use it in GitHub Desktop.
Save HappyCodingRobot/798519deea1a98aab277cc050a49df6f 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