Let’s learn a bit about how Python keeps track of names
Namespaces are the way that Python organizes variable names
For example, suppose I write a script math2.py like this:
# Filename: math2.py
pi = 'foobar'
Now I start the Python interpreter and import it
>>> import math2
Next I import the math module from the standard library
>>> import math
Both of these modules have an attribute pi:
>>> math.pi
3.1415926535897931
>>> math2.pi
'foobar'
How is it that Python does not get confused with these two pi?
Whenever Python executes a module, it creates a namespace for that module
In Python, namespaces are implemented as dictionaries
We can look at the dictionary directly, using moduleName.__dict__:
>>> import math
>>> math.__dict__
{'pow': <built-in function pow>, ..., 'pi': 3.1415926535897931,...}
This namespace is created when we execute import math
When we access elements of the namespace using the dotted attribute notation
>>> math.pi
3.1415926535897931
this is in fact equivalent to math.__dict__['pi']
>>> math.__dict__['pi'] == math.pi
True
As we saw above, the math namespace can be printed by typing math.__dict__
Another way to see its contents is to type vars(math)
>>> vars(math)
{'pow': <built-in function pow>,...
Finally, if you just want to see the names, you can type
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',...
Notice the special names __doc__, __file__ and __name__
These are initialized in the namespace when any module is imported
>>> print math.__doc__
This module is always available. It provides access to the
mathematical functions defined by the C standard.
>>> math.__file__
'/usr/lib/python2.5/lib-dynload/math.so'
>>> math.__name__
'math'
In Python, commands typed at the prompt >>> are regarded as part of a module called __main__
An assignment such as
>>> x = 4
is stored in the namespace of __main__
To see the contents of __main__ use vars() rather than vars(__main__)
>>> vars() # After starting Python, before making any assignments
{'__builtins__': <module '__builtin__' (built-in)>,
'__name__': '__main__',
'__doc__': None}
Now let’s make an assignment
>>> x = 4
>>> vars()
{'__builtins__': <module '__builtin__' (built-in)>,
'__name__': '__main__',
'__doc__': None,
'x': 4}
The variable x has been registered in the namespace
If we want to see just the names in __main__, we can use dir()
>>> dir()
['__builtins__', '__doc__', '__name__', 'x']
Next, suppose we have a file called mod1.py with the following code:
x = 3
def f():
pass # 'pass' means do nothing
To use this code, one option is to import it
>>> import mod1
In this case, the code in the script is regarded as part of the module mod1.py
Another option is to run it interactively
In this case, the code in the script is regarded as part of __main__
Another way to see the difference between running as __main__ and importing:
Let’s say we have a script mod2.py as follows
# Filename: mod2.py
print __name__
Now let’s look at two different ways of running it in IPython
In [1]: import mod2 # Standard import
mod2
In [2]: run mod2.py # Run interactively
__main__
In the second case, the code is executed as part of __main__, so __name__ = '__main__'
Suppose we are working interactively, at the prompt >>>
When we import a module,
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'math']
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',...
We can also import names directly into the current namespace using from
>>> from math import pi, e
>>> pi
3.1415926535897931
>>> e
2.7182818284590451
>>> dir()
['__builtins__', '__doc__', '__name__', 'e', 'math', 'pi']