Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A global variable in Python is a variable defined at the module level, outside of any function or class definition. Because Python does not implement block-level scoping for control flow statements, variables initialized inside if, for, while, or with blocks at the module level are also treated as global variables. Once initialized, a global variable is bound to the module’s global namespace and can be referenced from any execution context within that same module. Under the hood, Python stores global variables in a dictionary associated with the module, which can be inspected using the built-in globals() function. During variable resolution, Python follows the LEGB (Local, Enclosing, Global, Built-in) rule, checking the global namespace if a variable is not found in the local or enclosing scopes.

Reading Global Variables

When a variable is referenced but not assigned a value within a local scope, Python automatically resolves it to the global namespace.
x = 100  # Global variable

def read_global():
    # Python resolves 'x' in the global namespace (LEGB rule)
    print(x) 

read_global()  # Output: 100

Variable Shadowing (Implicit Local Binding)

If an assignment operation (=) is performed on a variable name inside a local scope, Python’s default behavior is to create a new local variable in that function’s namespace. This local variable shadows the global variable of the same name for the duration of the function’s execution. The global variable remains unmodified.
y = 200  # Global variable

def shadow_global():
    y = 50  # Creates a new local variable 'y', shadowing the global 'y'
    print(y)

shadow_global()  # Output: 50
print(y)         # Output: 200 (Global remains unchanged)

Lexical Scoping and UnboundLocalError

Python determines variable scope lexically at compile time. If a variable is assigned a value anywhere within a local scope, Python treats it as a local variable for that entire scope. Attempting to read the variable before the assignment occurs will not resolve to the global namespace; instead, it raises an UnboundLocalError.
count = 10  # Global variable

def increment_counter():
    # Python sees 'count' is assigned later in this block, 
    # making it a local variable for the entire function.
    # Reading it here raises an UnboundLocalError.
    print(count) 
    count += 1   

increment_counter()  # Raises: UnboundLocalError: cannot access local variable 'count' where it is not associated with a value

Modifying Global Variables: The global Keyword

To modify an existing global variable from within a local scope (and avoid UnboundLocalError), you must explicitly declare the variable using the global keyword before assigning a value to it. This directive instructs the Python interpreter to bypass local namespace binding and map the assignment directly to the module-level namespace.
z = 300  # Global variable

def modify_global():
    global z     # Instructs interpreter to bind 'z' to the global namespace
    z += 50      # Modifies the global variable directly

modify_global()
print(z)         # Output: 350

Cross-Module Global Variables

Python does not have true cross-file global variables by default. A global variable is strictly scoped to the module (file) in which it is defined. To access or modify a global variable from another module, the variable must be accessed as an attribute of the imported module object.

# config.py
timeout = 60


# main.py
import config


# Accessing the global variable from config.py
print(config.timeout)  


# Modifying the global variable in config.py's namespace
config.timeout = 120   
Master Python with Deep Grasping Methodology!Learn More