Global statement
A global statement is a Python declaration that makes specified names in a function refer to bindings in the module-level global namespace.
Python How-To
The global keyword declares that assignments to a name in a function refer to the corresponding binding in the global scope.
- How it is used
- Uses a function to update a module-level configuration value or other global binding.
- How deep it goes
- Explains LEGB resolution, local namespaces, and successful assignment without descending into interpreter internals.
- Examples
- Demonstrates changing db_filename and selecting the app_db database option.
- What you can do after
- Apply it to update a global variable from a function and diagnose why an unqualified assignment stays local.
Python Workout, Second Edition
The global statement declares that assignments and name accesses in a function refer to a module-level global name.
- How it is used
- Declares that a function's name retrievals and assignments target a module-level global variable.
- How deep it goes
- Distinguishes local assignment from global assignment and explains why list or dictionary item mutation differs.
- Examples
- Uses contrasting `x` examples showing local shadowing and explicit global reassignment.
- What you can do after
- Apply it via Python syntax to update a module-level name from inside a function when necessary.
Read how Python Workout, Second Edition explains global statement →
Python Workout: 50 ten-minute exercises
The global statement declares that assignments and references to specified names in a function use the module-level global bindings.
- How it is used
- Controls whether function assignments and references target local names or module-level global bindings.
- How deep it goes
- Explains declaration behavior with runnable examples and cautions against global mutation, without developing implementation internals.
- Examples
- Uses contrasting x bindings and a function that changes x through global.
- What you can do after
- Apply it via Python to update a module-level binding from inside a function when necessary.
Read how Python Workout: 50 ten-minute exercises explains global statement →
Related concepts
- global variableconfigures · A global variable is a variable whose binding is accessible from broad program or module scope according to the language’s name-resolution rules.
- nonlocal statementcontrasts with · A nonlocal statement makes a name assigned in a nested Python function refer to a binding in an enclosing function scope.
- bindingconfigures · Name binding is the association of an identifier with the program entity or value it denotes.
- scopeuses · Scope is the region of a program in which names are visible and can be resolved according to the language’s lookup rules.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


