pythonfunctionglobal

Python not using global variable in function even when global command is used


I am trying to change the value of a global variable in a function, however this is not happening even when "global" is specified before attempting to use/alter the variable.

Here is an abstraction of what I did (real code is quite long):

testVariable = 0

def functionExample():
     global testVariable
     # other code
     testVariable = 5
     print(testVariable) # this prints 5

functionExample()
print(testVariable) # this prints 0

If you run this code yourself, it will probably correctly assign 5 to "testVariable". However, in my code it does not do this. Here is the actual code if this helps (the code is meant for a strategy game):

keepPos = ("N/A","N/A")

def createBuilding3x3(mPos,coinCounterLocal,mouseSquareX,mouseSquareY,selectedBuilding,keepPlaced):
    global keepPos
    if keepPlaced != True:
        gridSquareEmpty = True
        for x in range (-1,2):
            for y in range (-1,2):
                if SSA[mouseSquareX+x][mouseSquareY+y] != 2:
                    gridSquareEmpty = False
                for i in buildingGroup:
                    if (mouseSquareX+x,mouseSquareY+y) == (i.getTileX(),i.getTileY()):
                        gridSquareEmpty = False
        if gridSquareEmpty == True:
            for x in range (-1,2):
                for y in range (-1,2):
                    if (x,y) != (0,0):
                        invisibleBuilding = Building(mouseSquareX+x,mouseSquareY+y,invisibleImage,0)
                        buildingGroup.add(invisibleBuilding)
            newBuilding = Building(mouseSquareX,mouseSquareY,imageList[selectedBuilding],0)
            buildingGroup.add(newBuilding)
            keepPos = (mouseSquareX,mouseSquareY)
            print(keepPos) # this returns (mouseSquareX,mouseSquareY)
            print("The amount of active buildings is",len(buildingGroup))
            return True
    if keepPlaced == True:
        return True
    return False

# the function is run during the program, after which:
print(keepPos) # this returns ("N/A","N/A")

Is my python cursed? Or have I just overlooked something? Thank you if you have any ideas as to what is wrong. Sorry if this is a duplicate, but I couldn't find any answers.


Solution

  • buildingPlacementProcedures.py contains this assignment at the outermost level:

    keepPos = ("N/A","N/A")
    

    That module also assigns to that variable inside a function:

    def createBuilding3x3(...):
        global keepPos
        ...
        if gridSquareEmpty == True:
            ...
            keepPos = (mouseSquareX,mouseSquareY)
    

    The module P2.py contains this import:

    from buildingPlacementProcedures import *
    

    ... which imports the keepPos variable and the createBuilding3x3() function, among many others.

    However, this imports only the initial value of keepPos. Any further changes to keepPos that occur inside the buildingPlacementProcedures module (such as when createBuilding3x3() is called from P2.py) will not be visible inside the P2.py module.

    That is the source of your confusion. It seems you expected global to apply program-wide, but it does not; it only applies module-wide.

    If you want to keep track of the changing value of a global variable inside another module, you have to refer to it explicitly using the namespace of that module.

    i.e., you need something like this:

    import buildingPlacementProcedures
    
    print(buildingPlacementProcedures.keepPos)