Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 is dropped, removing all local variables and reducing the reference count on all objects assigned to those local variables.

Additionally, the types module includes the SimpleNamespace class. An instance of this class allows us to build...