我目前无法在 Python 中修改另一个文件中的静态变量。
文件A
class MainClass(object):
global_var = 0
def __init__(self):
MainClass.global_var = 1
class SecondClass(object):
def __init__(self):
MainClass.global_var = 2
if __name__ == '__main__':
main = MainClass()
print "After MainClass:" + str(MainClass.global_var)
second = SecondClass()
print "After SecondClass:" + str(MainClass.global_var)
from FileB import ThirdClass
third = ThirdClass()
print "After ThirdClass:" + str(MainClass.global_var)
文件B
class ThirdClass(object):
def __init__(self):
from FileA import MainClass
MainClass.global_var = 3
输出
After MainClass:1
After SecondClass:2
After ThirdClass:2
我想在 FileB 中将 MainClass 中的静态变量修改为 3。我究竟做错了什么?谢谢!