The Python namespace concept

We've already seen two applications of the Python namespace. When we assign variables at the >>> prompt, we're introducing the variable into the global namespace. When we import a module, the module creates its own namespace within the global namespace.

That's why we can then use qualified names like math.sqrt() to refer to objects inside the module's namespace.

When we look at functions and class definitions, we'll see additional use, of namespaces. In particular, when evaluating a function or a class method, a local namespace is created, and all variables are part of that local namespace. When the function evaluation finishes (because of an explicit return statement or the end of the indented block,) the local namespace ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.