Python namespaces: Local, nonlocal, global

Together with my co-authors I am proud to see that we have achieved a substantial amount of coding examples and use cases on this blog. Our contribution related to Python are growing. For this reason I want to keep publishing short documentations explaining some important aspects of Python programming. In this post I cover namespaces in Python and why it is important to understand them.

Introduction to Python namespaces

In Python, a namespace is a mapping of names to objects. For example, the local names within the body of a function form a namespace. Attributes of an objects also form a namespace.

There is no relationship between namespaces in Python. That is, a name, e.g. a function name, can exist in several namespaces independently. Namespaces have individual lifetimes and they can be created at different moments in time.

One namespace is the namespace of built-in functions. This is a namespaces that is never deleted, being created when Python starts its interpreter. Local namespacesfor a function, on the other hand, are created when the function is called and deleted when the function returns (or throws an exception). Recursive function calls each have their own local namespace.

Python scope definitions and namespaces

In Python, the determination of a scope is static but ther usage is dynamic. A scope represents the namespace area directly accessible to a Python program.

There are 4 scopes directly accessible to a Python program:

  • innermost scope
  • scopes of enclosing functions
  • global scope, i.e. current module’s global names
  • outermost scope with builtin names

The namespaces of these scopes are defined in above order, i.e. starting with innermost scope and finally searching the outermost scope.

The built-in namespace in Python

In addition to the namespaces listed above it is worthwhile mentioning the built-in namespace. This namespace is always available in Python. It comprises core datatypes such as e.g. error and exception classes, but also e.g. basic functions such as range(). If you are used to programming in R then you might want to compare the built-in namespace in Python to the base package in Python.

Assignments to namespace scopes

By default Python assignments are put into the innermost scope. In general, all operations use the local scope and assign to it. This is e.g. also true when importing from a module. The import statement “places” the module or its functions in the local scope.

As stated variables are assigned to the innermost local scope by default. Variable assignments can be rebound, using the nonlocal or global statement.

Using the nonlocal statement will rebound variables to the enclosing scope. Using the global statement will rebound variables to the global scope.

You May Also Like

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.