请看下面的例子:Python:如何将多个参数传递给属性getter?
class A:
@property
def x(self): return 5
所以,当然调用a = A(); a.x
将返回5
但是想象一下,你希望能够修改属性x。
这种方式,例如:
class A:
@property
def x(self, neg = False): return 5 if not neg else -5
而且随着a = A(); a.x(neg=True)
这将引发TypeError调用它:'int' object is not callable
,这是很正常的,因为我们的x
作为5
评估。
所以,我想知道如何将更多的参数传递给属性获取器,如果可能的话。
我知道!你可以在我的第一个例子中看到'a.x'调用。 – Rizo 2011-04-19 11:31:22
@Rizo:属性不是为此设计的。 – XORcist 2011-04-19 11:46:57