【发布时间】:2013-12-28 16:28:49
【问题描述】:
我正在阅读“绝对初学者的 Python 编程”,其中有一段代码:
@property
def mood(self):
unhappiness = self.hunger + self.boredom
if unhappiness < 5:
m = "happy"
elif 5 <= unhappiness <= 10:
m = "okay"
elif 11 <= unhappiness <= 15:
m = "frustrated"
else:
m = "mad"
return m
它所做的只是即时计算并返回计算结果。它不提供对私有属性或类的任何属性的访问。这是作为属性而不是方法更好吗?
【问题讨论】:
标签: python properties