Last active
February 4, 2025 08:53
-
-
Save Dunes/2a4884091e7d249fa886ec0b13fb4079 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
Example showing that attribute lookups always require consulting the class, | |
even if an attribute is found in the instance __dict__. This is due to data | |
descriptors taking prescedence over instance attributes. | |
""" | |
class DataDescriptor: | |
def __get__(self, inst, cls=None): | |
return 'descriptor' | |
def __set__(self, inst, value): | |
pass | |
class MyClass: | |
def __init__(self): | |
self.foo = 'instance' | |
obj = MyClass() | |
assert obj.foo == 'instance' | |
MyClass.foo = DataDescriptor() | |
assert obj.foo == 'descriptor' | |
assert obj.__dict__['foo'] == 'instance' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment