3 回答
TA贡献2011条经验 获得超2个赞
你没有调用函数。如果你这样做了,你会得到一个 TypeError
应该是这样的
test = 0
class Testing(object):
@staticmethod
def add_one():
global test
test += 1
Testing.add_one()
TA贡献1796条经验 获得超10个赞
您应该调用该方法。那么只有它会增加变量的值test。
In [7]: test = 0
...: class Testing:
...: def add_one():
...: global test
...: test += 1
# check value before calling the method `add_one`
In [8]: test
Out[8]: 0
# this does nothing
In [9]: Testing.add_one
Out[9]: <function __main__.Testing.add_one()>
# `test` still holds the value 0
In [10]: test
Out[10]: 0
# correct way to increment the value
In [11]: Testing.add_one()
# now, check the value
In [12]: test
Out[12]: 1
TA贡献1829条经验 获得超7个赞
试试这个,
test = 0
class Testing:
def add_one(self):
global test
test += 1
print(test)
t = Testing()
t.add_one()
添加回答
举报