Time for the details
These are commonly used terms
Let’s try to understand the definitions
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
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}
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...
When we reference a name, how does the Python interpreter find the corresponding value?
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
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
The global namespace {} is created
Local namespace {} is constructed
After computations, return value assigned to y
Local x and a are discarded (namespace is deallocated)
Note that global a was not affected by local a
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
f is registered as a function in the global namespace
x bound to 1 in the global namespace
Creates a local namespace
Adds x to local namespace, bound to 1
Returns the value of local x
Local namespace deallocated, local x lost
Prints the return value 2 for f(x), and the value 1 for global 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
f is registered as a function in the global namespace
x bound to [1] in the global namespace
Global x has been modified
Conclusion: functions can modify global variables if they are mutable