Name spaces and modules
-
Name spaces, or environments, are the places where variables are stored.
-
Because a variable is a relationship between a symbol (name) and an object,
name spaces can also be seen as mappings from symbols to objects.
Name spaces are implemented as dictionaries in Python.
- The most important thing to know about name spaces is that there is no relationship between the same name in different name spaces.
So you can have two different functions with the same name in different modules without confusing them.
- Anything that appears after a dot is an attribute of the thing before the dot.
What appears before the dot is something that defines a name space, and the attribute is a variable
with a value in that name space.
-
Each module defines a name space.
-
One module that is part of every Python program is
__builtins__.
This is where functions such as str, len, and
max are stored.
This name space is created when the Python interpreter is started up.
-
Each source file also defines a module.
The module name space maps each of the symbols representing functions and other objects in the module to the objects themselves.
These variables are called "global".
The module's name space is created when the module is imported.
- Here is an example of two modules.
The module
evolve imports the module mutate.
Because the module mutate has a function in it called
mutate, this function can be accessed in evolve
with mutate.mutate.
-
The import statement
from <module> import * imports all of the global variables into the current name space.
So if we do from random import *, we can call the function random() directly
rather than calling it as random.random().
-
Executing the "run" command on a module in IDLE imports all of the variables in the module into a name space called
__main__.
Name spaces and functions
-
Each function also has its own name space, with variables that are "local".
These include the parameters of the function and any variables that are defined within the body of the function.
-
A local name space for a function is created whenever the function is called; the name space then goes away (is "forgotten") when the function returns.
-
Each object also defines its own name space (more on this later).
Name spaces, variables, and parameters
-
Here is another example illustrating name spaces, variables, and parameter passing:
parampass.py.
- Here's what happens when the function
mix_up is called.
-
The value of the global variable
i does not change because it is the value of the parameter i in the function mix_up that is being incremented. For the same reason, the value of the global variable string does not change.
- When
mix_up is called, its parameters get the values that i and string have where the call takes place.
-
The statement
p_one = p_two makes the pointer p_one (originally pointing to where p1 points) point to the same place as p_two (that is, to where p2 points).
-
Then the values associated with the keys
age and name in that dictionary (now pointed to by p_one, p_two, and p2) are modified.
-
So the value of the global variable
p1 remains the same because it was never modified, while the object pointed to by p2 is changed (though it is still the same object).
- A diagram of how the pointers change as the program runs: