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 →
Also covered in
What to know first
- decoratorA decorator is a programming-language construct that adds metadata or behavior to, or transforms, a program declaration, often a function or class.
- namespaceA global namespace is a top-level naming context that binds identifiers defined at the outermost level and distinguishes them from names in nested scopes.
- assignmentAssignment is a programming-language operation that binds or stores an evaluated value in a variable, property, or other assignable location.
Related concepts
- statementkind of · A statement is a syntactic unit in a programming or query language that expresses an instruction, declaration, or control operation for a processor to execute.
- global variableconfigures · A global variable is a variable whose binding is accessible from broad program or module scopes 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 to their bindings according to the language’s rules.
Questions about global statement
What is 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.
Which Manning books explain global statement?
Python How-To, Python Workout, Second Edition and Python Workout: 50 ten-minute exercises.
What should I know before learning global statement?
Manning books that teach global statement usually introduce decorator, namespace and assignment first.
What is the difference between global statement and nonlocal statement?
A global statement is a Python declaration that makes specified names in a function refer to bindings in the module-level global namespace. A nonlocal statement makes a name assigned in a nested Python function refer to a binding in an enclosing function scope.
What is global statement a kind of?
Global statement is a kind of statement. A statement is a syntactic unit in a programming or query language that expresses an instruction, declaration, or control operation for a processor to execute.
What is global statement also called?
Global statement is also called global keyword.
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.


