Why does python descriptor __set__ not get called
I have a descriptor on a class, and it's __set__ method does not get
called. I have been looking long and hard on this for a few hours and have
no answer for this. I'm pretty new to python. But what I noticed below is
that I assign 12 to MyTest.X, it erases the property descriptor for X, and
replaces it with the value of 12. So the print statement for the Get
function gets called. That's good.
But the print statement for the __set__ function does NOT get called at
all. Am I missing something?
class _static_property(object):
''' Descriptor class used for declaring computed properties that don't
require a class instance. '''
def __init__(self, getter, setter):
self.getter = getter
self.setter = setter
def __get__(self, instance, owner):
print "In the Get function"
return self.getter.__get__(owner)()
def __set__(self, instance, value):
print "In setter function"
self.setter.__get__()(value)
class MyTest(object):
_x = 42
@staticmethod
def getX():
return MyTest._x
@staticmethod
def setX(v):
MyTest._x = v
X = _static_property(getX, setX)
print MyTest.__dict__
print MyTest.X
MyTest.X = 12
print MyTest.X
print MyTest.__dict__
No comments:
Post a Comment