Namespaces
Dave Angel
davea at ieee.org
Fri Jan 21 08:01:59 EST 2011
On 01/-10/-28163 02:59 PM, sl33k_ wrote:
> What is namespace? And what is built-in namespace?
>
A namespace is a mapping from names to objects. When you write a statement
xyz = 42
the system looks up "xyz" in some namespace and associates that
"variable" with the object int(42).
The key is that there are multiple namespaces defined. The built-in
namespace (containing things such as open, help, next, input, and lots
more) is always available. The global namespace, for symbols defined
globally in the current module, is another namespace. If you're inside
a function, there's a separate namespace for symbols defined in there
(and they behave just a little differently). And you can explicitly
specify a namespace with a prefix, which is one way you access symbols
in another module, or within an instance of an object.
Perhaps look at:
http://bytebaker.com/2008/07/30/python-namespaces/
though I haven't personally studied the whole thing for accuracy.
One other thing: dir() can be used to show you the names in a
particular namespace. For example, dir(__builtins__) shows you the
built-in namespace, while dir() shows you the global one. And after an
import, dir() can show you those names:
import os
dir(os)
DaveA
More information about the Python-list
mailing list