class Silly:
    @property
    def silly(self):
        "This is a silly property"
        print("You are getting silly")
        return self._silly

    @silly.setter
    def silly(self, value):
        print("You are making silly {}".format(value))
        self._silly = value

    @silly.deleter
    def silly(self):
        print("Whoah, you killed silly!")
        del self._silly

s = Silly()
s.silly = "funny"
value = s.silly
del s.silly

但它并没有像预期的那样打印“你越来越傻了”、“你让傻笑了”……。不知道为什么。你能让我弄清楚吗,伙计们?

提前致谢!

最佳答案

The property decorator 仅适用于 new-style classes ( see also )。使 Silly 显式地从 object 扩展,使其成为新式类。 (在 Python 3 中,所有类都是新式类)

class Silly(object):
    @property
    def silly(self):
        # ...
    # ...

10-05 21:54