首页 > 解决方案 > 我无法在静态类中创建 python 属性

问题描述

我正在尝试为静态类中的变量创建一个 python 属性。我做了一个简化的尝试:

class c(object):
    _a = 5

    @staticmethod
    def a_get():
        print 'getter'
        return self._a

    @staticmethod
    def a_set(new_value):
        print 'setter'
        self._a = new_value

    a = property(a_get, a_set)

def main():
    print c.a

if __name__ == '__main__':
    main()

我期望发生的事情-它将打印getter 5

实际发生的情况 - 它打印属性本身(<0x000000000308F1D8> 处的属性对象)。

我错过了什么?

标签: pythonpython-2.7properties

解决方案


推荐阅读