Namespaces

Alex Willmer alex at moreati.org.uk
Fri Jan 21 06:55:39 EST 2011


On Jan 21, 10:39 am, sl33k_ <ahsanbag... at gmail.com> wrote:
> What is namespace? And what is built-in namespace?

A namespace is a container for names, like a directory is a container
for files. Names are the labels we use to refer to python objects
(e.g. int, bool, sys), and each Python object - particularly modules
and classes - provides separate namespace.

The idea of a namespace is to isolate names from one another - so that
if you import module_a and module_b and both have an object called foo
then module_a.foo doesn't interfere with module_b.foo.

The built-in namespace is where the core objects of Python are named.
When you refer to an object such as int Python first searches the
local scope (was it defined in the current function/method, i.e. the
output of locals()), then module scope (was it defined in the
current .py file, i.e. output of globals()) and finally in the object
__builtins__.

Hope that makes sense. I realised as I typed this my understanding of
Python namespaces is not as 100% tight as I thought.

Alex



More information about the Python-list mailing list