在
property从一类访问时(即,当描述符总是返回本身
instance是
None在它的
__get__方法)。
如果这不是您想要的,则可以编写一个始终使用类对象(
owner)而不是实例的新描述符:
>>> class classproperty(object):... def __init__(self, getter):... self.getter= getter... def __get__(self, instance, owner):... return self.getter(owner)... >>> class Foo(object):... x= 4... @classproperty... def number(cls):... return cls.x... >>> Foo().number4>>> Foo.number4


![如何在Python中创建只读类属性?[重复] 如何在Python中创建只读类属性?[重复]](http://www.mshxw.com/aiimages/31/669678.png)
