3 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
你沒有調(diào)用函數(shù)。如果你這樣做了,你會(huì)得到一個(gè) TypeError
應(yīng)該是這樣的
test = 0
class Testing(object):
@staticmethod
def add_one():
global test
test += 1
Testing.add_one()

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
您應(yīng)該調(diào)用該方法。那么只有它會(huì)增加變量的值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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
試試這個(gè),
test = 0
class Testing:
def add_one(self):
global test
test += 1
print(test)
t = Testing()
t.add_one()
添加回答
舉報(bào)