These two codes will give two different outputs, why ?
class Test:
def __get__(self, instance, owner):
return 42
def __set__(self, instance, value):
pass
class A:
a = Test()
a = A()
print(a.a) // print 42
a.a = 0
print(a.a) // print 42
And
class Test:
def __get__(self, instance, owner):
return 42
def __set__(self, instance, value):
pass
class A:
pass
a = A()
a.a = Test()
print(a.a) // print <__main__.Test object at 0xb700d6cc>
a.a = 0
print(a.a) // print 0
How are stored attributes in the Python engine ?
Your
Testclass isn’t called “attribute”, it’s a descriptor. Descriptors work, by definition, only when stored on a (new style, for the poor Python 2 users) class. Descriptor objects on non-class objects have no special meaning, they are treated like any other object. Their__get__method is ignored when they are retrieved, and their__set__is ignored when they are replaced.