Suppose we define the following python descriptor
class FooDescriptor():
def __init__(self, name='foo', initval='initval'):
self.name = name
self.val = initval
def __get__(self, instance, cls):
print("invoke get {} in instance {} in class {}".format(self.name, instance, cls))
return self.val
def __set__(self, instance, val):
print("invoke set {} in instance {}".format(self.name, instance))
self.val = val
Now if we define the class Foo
to have a class attribute of type FooDescriptor
and the instance attribute of the same type:
class Foo():
cdescr = FooDescriptor(name='class attribute descr', initval=1)
def __init__(self):
self.idescr = FooDescriptor(name='instance attribute descr', initval=2)
We get the desired behaviour for the class descriptor:
>>> foo = Foo()
>>> foo.cdescr
invoke get class attribute descr in instance <__main__.Foo object at 0x10189ecc0> in class <class '__main__.Foo'>
1
>>> foo.cdescr = 100
invoke set class attribute descr in instance <__main__.Foo object at 0x10189ecc0>
>>> foo.cdescr
invoke get class attribute descr in instance <__main__.Foo object at 0x10189ecc0> in class <class '__main__.Foo'>
100
However for the instance attribute idescr
, the descriptor object's __set__
and __get__
methods do not get invoked when attribute is accessed or set:
>>> foo.idescr
<__main__.FooDescriptor object at 0x10189ecf8>
>>> foo.idescr = 120
>>> foo.idescr
120
>>> type(foo.idescr)
<class 'int'>
Any answer for better understanding the scope of the descriptors and how python decides to invoke their __set__
and __get__
when attribute protocal .
is seen would be greatly appreciated.