What's a namespace? I hear you ask. Well, it's kinda hard to explain. Not because they are especially complicated, but because every language does them differently. The concept is pretty straightforward, a namespace is a space or region, within a program, where a name (variable, class etc) is valid.
They came about because early programming languages (like BASIC) only had Global Variables, that is, ones which could be seen throughout the program - even inside functions. This made maintenance of large programs difficult since it was easy for one bit of a program to modify a variable without other parts of the program realizing it - this was called a side-effect. To get round this, later languages (including modern BASICs) introduced the concept of namespaces. (C++ has taken this to extremes by allowing the programmer to create their own namespaces anywhere within a program. This is useful for library creators who might want to keep their function names unique when mixed with libraries provided by another supplier)
In Python every module creates it's own namespace. To access those names we have to either precede them with the name of the moduleor explicitly import the names we want to use into our modules namespace. Nothing new there, we've been doing it with the sys and string modules already. In a sense a class definition also creates its own namespace. Thus, to access a method or property of a class, we need to use the name of the instance variable or the classname first.
In Python there are only ever 3 namespaces (or scopes):
So far so good. Now how does this come together when variables in different namespaces have the same name? Or when a name not in the current namespace is referenced? Looking at the former situation first: If a function refers to a variable called X and there exists an X within the function (local scope) then that is the one that will be seen and used by Python. It's the programmers job to avoid name clashes such that a local variable and module variable of the same name are not both required in the same function - the local variable will mask the global.
In general you should minimize the use of 'global' statements, it's usually better to pass the variable in as a parameter and then return the modified variable.
The second point, where a name which is not within the current local namespace is referenced, is resolved as follows: The function will look wihin its local namespace, if it can't find it there it will then look at the module scope and if not there then at the builtin scope. The only snag with this is when we want to assign a value to the external variable. Normally this would create a new variable name, but we don't want that to happen so once more we must specify it as global to ensure we don't create a local version of the name.
We can see all of this at work in this example (which is purely about illustrating the point!):
# variables with module scope W = 5 Y = 3 #parameters are like function variables #so X has local scope def spam(X): #tell function to look at module level and not create its own W global W Z = X*2 # new variable Z created with local scope W = X+5 # use module W as instructed above if Z > W: # pow is a 'builtin-scope' name print pow(Z,W) return Z else: return Y # no local Y so uses module version
Now when we import a module such as sys we make the name sys available locally and then we can access names within the sys module namespace by qualifying the name as we've seen. If we do:
from sys import exit
we only bring the exit function into the local namespace. We cannot use any other sys names, not even sys itself.
BASIC takes the opposite approach to Python by making all variables global by default (for compatibility with old BASIC programs) but allowing the programmer to create LOCAL variables within functions too.
So far as I can tell there are no namespace controls in Tcl. Possibly because of the unique way Tcl parses the program. In any case it seems that all variables are local to their immediate surroundings - file level variables are only visible to commands within the file and procedure variables are only visible within the procedure. To communicate between the two namespaces you must pass the values in as parameters.
If you have any questions or feedback on this page
send me mail at:
alan.gauld@btinternet.com