Go to top, next, previous, or johnstachurski.net

Name Resolution

Time for the details

Global and Local Namespaces

These are commonly used terms

Let's try to understand the definitions

Global Namespaces

The global namespace is the namespace of the module currently being executed

To list this namespace, use globals()

For example, suppose that we start the interpreter and begin making assignments

>>> x = 3
>>> dir()
['__builtins__', '__doc__', '__name__', 'x']

We are now working in the module __main__

Hence the namespace for __main__ is now the global namespace

Next, we import a module called amodule

>>> import amodule

The interpreter

The namespace amodule.__dict__ is now the global namespace

Once execution of the module finishes, interpreter returns to the module from where the import statement was made

Now the namespace of __main__ is the global namespace

Local Namespaces

Suppose that inside a module we have access to a function f

def f(x):
    a = 2
    return a * x

Now we call the function

y = f(1)

The interpreter creates a local namespace for the function, and registers the variables in that namespace

Variables in the namespace are called local variables

After the function returns, the namespace is deallocated (lost)

We can view the contents of the local namespace with locals()

def f(x):
    a = 2
    print locals()
    return a * x

Now we call the function

y = f(1)
{'a': 2, 'x': 1}

The __builtins__ Namespace

We have been using some built-in objects (mainly functions)

How does access to these names work?

>>> dir()  
['__builtins__', '__doc__', '__name__']
>>> dir(__builtins__)
[... 'iter', 'len', 'license', 'list', 'locals', ...] 

We can access elements of the namespace as follows

>>> __builtins__.max
<built-in function max>

But __builtins__ is special, because we can access them directly as well:

>>> max
<built-in function max>
>>> __builtins__.max == max
True

The reason why this works is explained in the next section...

Name resolution

When we reference a name, how does the Python interpreter find the corresponding value?

The Process of Name Resolution

At any point of execution, there are two or three namespaces which can be accessed directly

If the interpreter is not executing a function call then the namespaces are

Suppose that we refer to a name

print x

The interpreter

If the interpreter is executing a function call

y = f(1)

Then the namespaces are

Now the interpreter

Consequences

Consider the script test.py

def g(x):
    a = 1
    x = x + a
    return x

a = 0
y = g(10)
print "a = ", a, "y = ", y

What happens when I run this script?

$ python -i test.py
a = 0 , y = 11
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

Note that global a was not affected by local a



Mutable Versus Immutable Parameters

Consider the following code segment

def f(x):
    x = x + 1
    return x
x = 1
print f(x), x 

Prints 2 as the value of f(x) and 1 as the value of x

Different story when we use a mutable data type such as a list:

def f(x):
    x[0] = x[0] + 1
    return x
x = [1]
print f(x), x 

Prints [2] as the value of f(x) and same for x

Conclusion: functions can modify global variables if they are mutable