Skip to content

Instantly share code, notes, and snippets.

@pmolodo
Created May 16, 2017 21:52
Show Gist options
  • Save pmolodo/748b8c191aeb167508d56ff499fee3ac to your computer and use it in GitHub Desktop.
Save pmolodo/748b8c191aeb167508d56ff499fee3ac to your computer and use it in GitHub Desktop.
Test __getattribute__ and descriptor __set__
class MyDescriptor(object):
def __init__(self, name, val):
self.name = name
self.theVal = val
def __get__(self, instance, owner):
print "Getting {} from {} (owner: {})".format(self.name, instance, owner)
return self.theVal
def __set__(self, instance, val):
print "Setting {} from {} to {!r}".format(self.name, instance, val)
self.theVal = val
class TestClass(object):
def __repr__(self):
return "TestClass<{}>".format(id(self))
def __getattribute__(self, item):
print "TestClass.__getattribute__: {!r}".format(item)
return object.__getattribute__(self, item)
foo = MyDescriptor("foo", 3)
myObj = TestClass()
print "-- myObj.foo = 7 --"
myObj.foo = 7
print "-- myObj.foo --"
print myObj.foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment