Python 如何将类属性转换为整数

Python 如何将类属性转换为整数,python,class,properties,Python,Class,Properties,我有以下课程: class temp_con(): def __init__(self): self.t = 0 @property def t(self): return self.t @t.setter def t(self,value): self.t = value 我需要使用它来与以下逻辑的数字进行比较: if num <= temp_con.t - 2: #dothing 如果n

我有以下课程:

class temp_con():
    def __init__(self):
        self.t = 0
    @property
    def t(self):
        return self.t
    @t.setter
    def t(self,value):
        self.t = value
我需要使用它来与以下逻辑的数字进行比较:

if num <= temp_con.t - 2:
    #dothing

如果num您在类上访问t,而不是在类的对象上

尝试:

q=temp\u con()

如果num您需要类的一个实例来使用属性,并且,正如其他答案中所指出的,您需要为对象变量使用不同的名称。尝试:

class temp_con():
    def __init__(self):
    self._t = 0
@property
    def t(self):
    return self._t
@t.setter
    def t(self,value):
    self._t = value

my_temp_con = temp_con()

if num <= my_temp_con.t - 2:
    pass
class temp\u con():
定义初始化(自):
自我评价。_t=0
@财产
def t(自我):
返回自我
@t、 塞特
def t(自身,值):
自我价值
my_temp_con=temp_con()

如果num,则需要为属性及其包装的属性使用单独的名称。一个很好的约定是使用前缀为
\uuu
的属性名作为属性名

class TempCon:
    def __init__(self):
        self._t = 0

    @property
    def t(self):
        return self._t

    @t.setter
    def t(self, value):
        self._t = value
然后可以访问该类实例上的属性

temp_con = TempCon()
print(temp_con.t)
temp_con.t = 5
print(temp_con.t)

返回self.t
不返回
0
,它返回函数
t
(它是一个属性获取程序)。您应该区分属性和属性getter。Set
self.\u t=0
这个问题似乎在多个方面混淆了类成员和类实例成员之间的区别。例如,
temp_-con
被声明为一个类,但在使用示例中,
temp_-con
似乎也被用作一个变量,该变量预期是一个实例。答案是前两个答案的组合。
temp_con = TempCon()
print(temp_con.t)
temp_con.t = 5
print(temp_con.t)