liveBook · Manning

Built-in namespace

As explained in The Quick Python Book, Fourth Edition

The built-in namespace is the namespace containing Python's predefined names, including built-in functions and exceptions.

The built-in namespace supplies Python’s predefined names, including functions such as len, max, and list and exceptions such as NameError. It is the namespace associated with __builtins__. section 10.7

When Python encounters an unqualified name, it searches in this order:

  • The local namespace, then the global namespace. section 10.7
  • The built-in namespace. If the name is absent there too, Python raises NameError. section 10.7

A local binding hides a built-in

list("Peyto Lake") returns a list of characters. After `list = [1, 3, 5, 7]`, the same call raises `TypeError: 'list' object is not callable`: the new binding is found before the built-in function. `del list` removes that binding and makes the built-in available again. section 10.7.1

Use `dir(__builtins__)` to inspect built-in names, and `help()` or an object’s `__doc__` to inspect documentation. For a suspected mistyped local name, `dir()` without an argument lists local identifiers. section 10.7.1

Methods also look up unqualified names through local, global, and built-in namespaces. That search is distinct from accessing instance, class, and superclass names through `self`. section 15.11

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains lookup order, contents, inspection, and shadowing with a worked failure and recovery, without detailing interpreter internals.
Examples
Uses `dir(__builtins__)` and a reassigned `list` that raises TypeError until `del list` removes the binding.
What it assumes
Defines namespaces as mappings from identifiers to objects while assuming familiarity with variables, functions, and modules.
What you can do after
Diagnose/debug with it when a built-in name is hidden, using `dir()` to inspect names and `del` to remove an accidental binding.
How it connects
Connects to local and global namespaces, NameError, function calls, and attribute access through self.

Also explained in

Compare how 3 books explain built-in 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.