How to use global variables in Python?

The declaration of your globals is wrong. You declare your globals as a description of how they should be calculated. That will just not work. The global keyword in python is used to bring a variable in the local scope. Then you can alter it. Like this:

def updateGlobals():
    global global_variable
    global_variable = "new value"

However, I would put them your globals in a class and add functions that will update the variables accordingly. I think people call this the singleton design pattern, but maybe you don’t need this in your little project.

Browse More Popular Posts

Leave a Comment