A function normally creates a local variable when it assigns to a name. If the function must update the module-level binding instead, declare that name with global before retrieving or assigning it. section 6.5
Updating a module-level variable
x = 100 def foo(): global x x = 200 print(x) print(x) foo() print(x) The output is 100, 200, and 200: foo changes the existing global x rather than creating a separate local x. section 6.5
Without global, a function can use the same name locally without affecting the outer binding. Changing global state is usually discouraged, but can be necessary for an update such as a configuration parameter. section 6.5


