liveBook · Manning

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.

How 3 Manning books explain it

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.

Read how Python How-To explains global statement →

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

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.