2

The global variable does not 'save' after the function is done being executed. You can run the code below and see the debug output.

Main file:

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)

What I expect:
local var mode = True
Mode2D = True
Mode: True

Result I get:
local var mode = True
Mode2D = True
Mode: False

gopro_2027
  • 95
  • 8
  • Or even: https://stackoverflow.com/questions/1977362/how-to-create-module-wide-variables-in-python – Jan Dec 06 '17 at 19:09
  • 1
    You've clobbered your namespace. `set2DMode` is still looking at `GlobalTest.Mode2D`, but your function is printing `Main.Mode2D`. Don't clobber namespaces. – juanpa.arrivillaga Dec 06 '17 at 19:09

0 Answers0