Skip to content

Instantly share code, notes, and snippets.

@Dunes
Last active February 4, 2025 08:53
Show Gist options
  • Save Dunes/2a4884091e7d249fa886ec0b13fb4079 to your computer and use it in GitHub Desktop.
Save Dunes/2a4884091e7d249fa886ec0b13fb4079 to your computer and use it in GitHub Desktop.
"""
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