I have a descriptor class X
here. I try to use the descriptor X
in another class Y
class X:
def __init__(self, value):
self.value = value
def __get__(self, instance, owner):
print('#### X.__get__ ####')
return self.value
def __set__(self, instance, value):
print('#### X.__set__ ####')
self.value = value
class Y:
def __init__(self):
self.x = X(10)
y = Y()
print(y.x)
y.x = 20
I was hoping that the statement print(y.x)
would invoke x.__get__
and the statement y.x = 20
would invoke x.__set__
, but it doesn't happen. When I run the above program, I just get this output.
<__main__.X object at 0x7fc65f947950>
Why were the descriptor methods not invoked?