Correct Use Of Global Variables In Python 3

81,163

Solution 1

In the first case the global keyword is pointless, so that is not correct. Defining a variable on the module level makes it a global variable, you don't need the global keyword.

The second example is correct usage.

However, the most common usage for global variables are without using the global keyword anywhere. The global keyword is needed only if you want to reassign the global variables in the function/method.

Solution 2

You need to use the global keyword in a function if you use the global variable in a way that would otherwise be interpreted as an assignment to a local variable. Without the global keyword, you will create a local variable that hides the global in the scope of the function.

Here are a few examples:

global_var = 1

def example1():
    # global keyword is not needed, local_var will be set to 1.
    local_var = global_var

def example2():
    # global keyword is needed, if you want to set global_var,
    # otherwise you will create a local variable.
    global_var = 2

def example3():
    # Without using the global keyword, this is an error.
    # It's an attempt to reference a local variable that has not been declared.
    global_var += 1

Solution 3

"in a way that would otherwise be interpreted as an assignment to a local variable" --- yes, but here is a subtle detail:

------------------- error: local variable 'c' referenced before assignment

def work():
  c += 3

c = 0

work()
print(c)

------------------- error: local variable 'c' referenced before assignment

c = 0

def work():
  c += 3

work()
print(c)

------------------- prints [3]

def work():
  c.append(3)

c = []

work()
print(c)

------------------- prints [3]

c = []

def work():
  c.append(3)

work()
print(c)

Solution 4

The main difference between the first two cases and the next two cases in the above answer would have to be the fact that the list is mutable. For cases like a = 1 a pointer points to the location where 1 is and when you say a = 2 the pointer shifts.

For the case of mutable objects a memory location is allotted and when methods like append are used changes occur to the memory location itself and so the value the mutable references is changed globally.

Now the big question is as to how the function knows the variable we are modifying is a global one or local one because it seems we can modify the global variable if its mutable and we cannot if its non mutable (The function also does not recognize this as the global variable)

Share:
81,163
Eden Crow
Author by

Eden Crow

Updated on July 09, 2022

Comments

  • Eden Crow
    Eden Crow almost 2 years

    Which is the correct use of global variables in Python 3?:

    1) Stating global VAR_NAME once in the core script (not within a function) and then simply referring to the variable as VAR_NAME everywhere else

    2) Stating global VAR_NAME once within every function that uses the global variable and then simply referring to the variable as VAR_NAME for the rest of the function and within the core script itself

  • Dagrooms
    Dagrooms over 5 years
    Thank you, this helped me figure out how to use a module as a singleton instead of making a class for it. I didn't realize that I shouldn't reassign module-level variables inside of functions.
  • FKEinternet
    FKEinternet almost 5 years
    I'm not downvoting, but I see a problem with this answer: The OP's #2 example is actually incorrect: The global keyword is not needed within a function unless the function is assigning a [new] value to the global variable. If a function only references (reads from) a global variable, the global keyword is not required.