Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

global

The global statement makes listed names refer to the module’s top-level namespace throughout the current scope. It is mainly needed when code inside a function assigns to a module-level variable.

How it works

Use global inside a function or other scope when an assignment must update a name in the module’s top-level namespace. The declaration applies throughout the current scope, so the name must not be read or assigned before its declaration there; at module level, global has no practical effect.

Syntax

global identifier ("," identifier)*

Parameters

NameTypeDescription
identifieridentifierOne or more identifiers whose bindings should be treated as global in the current scope.

Key points

  • It changes the binding interpretation of each listed identifier in the current scope.
  • Inside a function, assigning to a module-level name requires declaring that name global first.
  • A function that only reads a module-level name generally does not need a global declaration.
  • The declaration is scope-wide rather than limited to the statement or line where it appears.

Common mistakes

  • Place global before every use or assignment of that name in the same scope; otherwise Python raises SyntaxError.
  • Do not add global at module level expecting it to change behavior; module-level names are already global.
  • A global declaration in dynamically supplied code does not alter the surrounding code, and surrounding declarations do not alter that dynamic code.

Example: Updating a module-level variable

counter = 0

def increment():
    global counter
    counter += 1

increment()
print(counter)  # 1

The function declares that assignments to counter target the module-level name, then increments it.

When to use it

Mutating module state

A function must increment or replace a module-level state variable.

Declaring multiple globals

Several names assigned in one function should resolve to module-level bindings.

Updating script-wide state

A script function needs to update a top-level configuration or cache reference.

Interview questions

QWhat problem does the global statement solve?

global tells Python to treat the listed names as bindings in the module’s top-level namespace rather than local bindings in the current scope. This allows an assignment such as counter += 1 inside a function to update the module-level variable.

Follow-up: What happens if the function only reads the name? A free variable can refer to a global without a global declaration; the declaration is needed for assignment in the current scope.

QWhat is the scope and ordering rule for global?

The declaration applies to the entire current scope, including a module, function body, or class definition. The listed name must not be used or assigned before its declaration in that scope.

Follow-up: Does global affect an enclosing function’s local variable? No. It refers to the top-level namespace, whereas nonlocal is used for a binding in an enclosing function scope.

QDoes global have an effect at module level or through exec()?

At module level, names are already global, so global has no effect there. It still cannot be used after an earlier use or assignment of the same name in that module scope.

Follow-up: What about exec()? A global statement in code passed to exec applies only to that separately parsed code and does not change the containing block.

  • nonlocal statement
  • exec()
  • eval()
  • compile()
  • top-level namespace

Source: 7. Simple statements. Checked 2026-10-16.