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

2
  • Or even: stackoverflow.com/questions/1977362/… Commented Dec 6, 2017 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. Commented Dec 6, 2017 at 19:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.