【发布时间】:2018-07-31 22:25:35
【问题描述】:
class Parrot:
def __init__(self):
self._voltage = 100000
@property
def voltage(self):
"""Get the current voltage."""
return self._voltage
我有上面的代码(找到here),我想了解不调用类并获得以下结果是否有任何可用性。
>>> Parrot.voltage
<property at 0x7f19e83cdf48>
我知道通常会是
>>> Parrot().voltage
100000
为了可用性,我的意思是Parrot.voltage有很多可用的@property方法,例如deleter, fdel, getter等。但是我不明白如何使用这些。
【问题讨论】:
-
创建对象有什么问题。如果没有对象类,则不会初始化,因此无法获取这些值
-
你是什么意思“任何可用性”?这只是属性实现方式的产物,作为类上的 descriptor 而不是实例上的属性。
-
p = Parrot只是让p成为Parrot的引用,因此x = p()将等同于x = Parrot()。
标签: python class memory properties attributes