我有一个属性是一个数字。我想要一个变量,即该值减去1:
class temp_conn:
def __init__(self):
self._t = 0
@property
def t(self):
return self._t
@t.setter
def t(self,value):
self._t = value
hist = temp_conn.t - 1
当我这样做时,它告诉我这是属性和int
的非法操作数,我无法将属性转换为int
。如何获得等效的temp_conn.t - 1
?
答案 0 :(得分:0)
您需要先启动课程
hist = temp_conn().t - 1
但是,您还有其他问题。执行上述代码将引发:
RecursionError: maximum recursion depth exceeded while calling a Python object
您需要更改self.t
,因为它与该属性共享一个名称,即该属性将继续自行调用(如上面的错误所示)。
例如:
In [3]: class temp_conn:
...: def __init__(self):
...: self._t = 0
...: @property
...: def t(self):
...: return self._t
...: @t.setter
...: def t(self,value):
...: self._t = value