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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access