Created
May 16, 2017 21:52
-
-
Save pmolodo/748b8c191aeb167508d56ff499fee3ac to your computer and use it in GitHub Desktop.
Test __getattribute__ and descriptor __set__
This file contains 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 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