我在一个类上有一个描述符,它的__set__方法没有被调用。几个小时以来,我一直在努力寻找这个问题,但对此没有任何答案。但是我在下面注意到的是,当我将 12 分配给 MyTest.X 时,它会删除 X 的属性描述符,并将其替换为值 12。因此调用了 Get 函数的打印语句。那挺好的。
但是__set__函数的打印语句根本不会被调用。我错过了什么吗?
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__