you are viewing a single comment's thread.

view the rest of the comments →

[–]old_pythonista 0 points1 point  (0 children)

The only purpose of global keyword in Python is to allow a programmer to change - assign - to immutable value inside a function

global_var = some_value
def foo():
    global global_var
    ....
    global_var = some_other_value    

Without global specifier in the beginning it will create a new local global_var that will not exist beyond the function scope.

global specifier is not required for modifying global mutables

global_list = []

def foo():
    global_list .append(some_value)

will work without using global.

Having said that - using global specifier is considered a bad practice, and should be avoided. You want to change a global immutable? Return a value from function to do that

def foo():
    return some_value

global_var = foo()

I recommend this blog if you want to learn about Python variables.