liveBook · Manning

Namespace

As explained in The Quick Python Book, Fourth Edition

A global namespace is the namespace associated with a module, script, or interactive session and visible as the defining environment for its functions.

A namespace maps names to objects. Assigning `x = 1` binds `x` to `1` in a namespace. Each module has its own namespace, so two modules can define `reverse` without a name clash: use `mymodule.reverse` or `othermodule.reverse` to choose one. section 10.7 section 10.1

Python looks for an unqualified name in the local namespace, then the global namespace, then the built-in namespace. If none contains it, Python raises `NameError`. At the top level of a module, script, or interactive session, local and global are the same namespace; assignments, function definitions, and imports add bindings there. section 10.7

A function call creates a local namespace for its parameters and variables. The function's global namespace belongs to where it was defined—not where it was called. A method follows the same direct local–global–built-in lookup; instance, class, and superclass names are instead reached through `self`. section 10.7 section 15.11

Inspect a function call

In `scopetest.py`, `v = 6` is a module-level binding. Inside `f(x)`, the assignment `y = x` creates a local binding, while `w = v` reads the module-level name. After `import scopetest`, calling `scopetest.f(2)` shows `x` on entry and `x`, `y`, and `w` on exit; its global listing contains the module's `v` and `f`, not the caller's names. section 10.7

Use these checks when a name is missing or unexpected:

  • Call `dir()` to list names in the current global namespace; pass a module or type to inspect its members. section 2.4
  • Call `globals()` and `locals()` to inspect bindings. At the top level they show the same bindings; inside a function, `locals()` shows its parameters and local variables. section 10.7
  • Account for session state: deleting an imported module name makes later use raise `NameError`, and earlier imports can affect package examples. Restart the session when a clean namespace is needed. section 10.7 section 18.3

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through lookup order and function-local bindings, while deferring further namespace details to Python documentation.
Examples
Uses identically named module functions, inspected function locals, and a deleted import that raises `NameError`.
What it assumes
Assumes familiarity with variables and functions while defining namespace mappings and lookup explicitly.
What you can do after
Diagnose/debug with it by inspecting `dir()`, `globals()`, and `locals()` to locate bindings and investigate missing names.
How it connects
Connects namespaces to modules, imports, function scope, built-in names, and class-instance attribute access.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.