[Tutor] What is a namespace? What is meant by "A namespace is a mapping from names to objects"

Alan Gauld alan.gauld at btinternet.com
Mon Dec 9 16:00:56 CET 2013


On 09/12/13 04:46, Varuna Seneviratna wrote:

> do not understand what is meant by "A /namespace/ is a mapping from
> names to objects". How I understand to be a namespace is a particular
> space within which a particular name is unique.

That's correct.
But a name on its own can refer to anything.
But it can't refer to nothing, in Python every name must refer to some 
kind of object for it to exist (even if the object is None).

So for python to know what a given name refers to it must have a mapping 
between name and object(data, function, class etc)

> space set of built-in names the name "abs()" is used to denote the
> function which returns the absolute value of a number and no other
> function(operation) can be named abs() as a built-in function.

That's not true. Names and objects (including functions) are only 
loosely bound together. I can easily create another name for the
abs function:

 >>> oldabs = abs
 >>> oldabs(5) == abs(5)
True

I can even name a completely different function 'abs'

 >>> def abs(n): return n

Now abs is a completely different function, but oldabs is still 
referring to the original built-in function.

Every name refers to exactly one object, but what object it refers to is 
not necessarily what it started out as. And there may be several names 
for the same object. So you need to maintain a mapping. And that mapping 
defines what the namespace contains.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list