liveBook · Manning

Namespace

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A named container that organizes identifiers and their bindings to prevent naming conflicts and control name resolution.

What happens when two useful functions need the same name? A namespace gives a name its context: it contains named things, including other namespaces. Think of an expense report called expenses.xlsx. A path through employee, date, and client directories distinguishes it from every other report without forcing all that detail into the filename. section 2.1.2 section 2.2

An address narrows the search

“Main Street” may occur in many cities, but the city gives the street name context. A mailing address continues narrowing that context until it identifies a person. Namespaces in an application serve the same purpose: short names remain meaningful because their location supplies the missing detail. section 2.2 section 2.3

Python provides several levels of namespace. Built-ins such as print() and open() are available throughout a program. Defining your own open() can shadow the built-in name, so a convenient name is not necessarily a safe one. A module—a Python code file—creates another namespace and lets related functionality live together rather than crowding one file. section 2.3.1 section 2.3.2

import utility 
 
def add(a, b):
    return a + b
 
print(add(12, 12))
print(utility.add(12, 12))

Two functions can both be named add when one belongs to the imported utility module. The calls add(12, 12) and utility.add(12, 12) select different functions; defining both in one module under the same name would instead lose the first definition. section 2.3.2

Functions add a narrower namespace, often described through scope. If a function uses prefix without defining it locally, Python can find the module-level prefix. Define prefix inside the function, and that local name takes precedence. A function-local variable exists only while that function runs; a module-level name can be accessed through the imported module. section 2.3.3 section 2.3.4

Keep a familiar call while changing its behavior

An email application needs formal, informal, and casual versions of get_name(person). Put each version in its own module, then select the appropriate module’s function for each recipient and bind it to the local name get_name. The subsequent calls can keep using get_name(person) instead of repeating a conditional wherever a name is needed. section 2.3.5

Namespaces also shape larger applications. Classes can create namespaces within modules. For a growing Flask application, modules separate functional areas, while Blueprints provide namespaces for URL endpoint functionality. Choose namespace names as carefully as the names inside them: the hierarchy should make related code easier to find and distinct behavior easier to select. section 2.4 section 4.2 section 7.4

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Works through built-in, module, and function namespaces, including shadowing, name lookup, scope, and variable lifetime.
Examples
Uses expense-report directories, mailing addresses, competing add functions, and personalized email names.
What it assumes
Assumes familiarity with Python functions and imports, while developing the namespace idea through directories and addresses.
What you can do after
Apply it via Python modules and Flask Blueprints to separate features and avoid naming conflicts.
How it connects
Connects namespaces to modules, functions, variable scope, classes, packages, and Flask Blueprints.

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 Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.