我之前已经看过有关此问题的问题,但是我无法在类函数中重新创建全局变量的更改:
val rddResult = rddKV1.subtractByKey(rddKV2)
当我输入
test = 0
class Testing:
def add_one():
global test
test += 1
它打印“ 0”。如何在类中获取要添加的功能以进行测试?
谢谢!
答案 0 :(得分:0)
您没有调用函数add_one
test = 0
class Testing:
def add_one():
global test
test += 1
Testing.add_one()
print (test)
答案 1 :(得分:0)
您没有调用该函数。如果这样做的话,您会收到TypeError
应该是这样
test = 0
class Testing(object):
@staticmethod
def add_one():
global test
test += 1
Testing.add_one()
答案 2 :(得分:0)
尝试一下
test = 0
class Testing:
def add_one(self):
global test
test += 1
print(test)
t = Testing()
t.add_one()
答案 3 :(得分:0)
您应该调用该方法。然后只有它会增加变量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