正在设置Python全局变量问题

时间:2017-12-06 19:05:01

标签: python

全局变量不会保存'函数完成后执行。您可以运行下面的代码并查看调试输出。

主文件:

from GlobalTest import *

def init():

    #call that function, should set Mode2D to True
    set2DMode(True)

    #define it as a global
    global Mode2D

    #see what the value is set to now, which is not what I set it to for an unknown reason
    print("Mode:",Mode2D)

init()

GlobalTest:

Mode2D = False

def set2DMode(mode):
    #define it as global so it uses the one above
    global Mode2D

    #set the GLOBAL variable to the argument
    Mode2D = mode

    #output of what it thinks it is, which is as expected
    print("local var mode =",mode)
    print("Mode2D =", Mode2D)

我的期望:
local var mode = True
Mode2D = True
模式:真实

结果我得到:
local var mode = True
Mode2D = True
模式:错误

0 个答案:

没有答案