Whats the difference between namespaces and names?

I'm assuming the namespace is the allotted place in memory in which the name is to be stored. Or are they the same thing?

Answers


A namespace is a theoretical space in which the link between names and objects are situated: that is what is called a mapping between the names and the objects. Names are the identifiers written in a script. Objects are structures of bits lying in the memory. The data structure that implements this theoretical namespace is a dictionnary. "Implements" means that it is the object that holds this data in the bits of the memory. But the objects that this dictionary references are not grouped all together in a delimited portion of the memory, they are lying everywhere in the memory, it's the role of the dictionary to know how to find any of them with just a name at start when a name is encountered by the interpreter. That's why I wrote it is a theoretical space, though it has a concrete existence in the memory. It is theoretical because the fact that several objects disseminated at different places in the memory can be considered to belong to one namespace is the result of the under-the-hood functionning of the Python interpreter, that is to say its data model and its execution model

In fact, things are more complex, under the hood there is a symbol table in the game. But I'm not enough competent concerning the C implementation of Python to say more. And by the way, people rarely allude to the symbol table.

However, I hope that the above explanation will shed some light in your mind concerning the subject of namespace.


Names are what can be traditionally thought of as "variables".

a = 1
b = 2

Both a and b are "names". If you try to reference a name that hasn't been set yet, you'll get a NameError:

>>> print c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined

Namespaces are the places where names live. Typically this is a module. Each module (file) has it's own namespace -- Or it's own set of names which map to a corresponding value (a->1, b -> 2 in my example above). You can "merge" module namespaces in different ways using python's import functionality.

import foo  # bring foo into the current namespace
from foo import *  # bring all of the names in foo's namespace into the current namespace

Classes can also be considered a namespace although you can't import the names directly into your module namespace without some work.

class Namespace(object):
    pass

namespace = Namespace()
namespace.a = 1
namespace.b = 2

As pointed out in some of the discussion in the comments, I ought to mention that namespaces in python are usually implemented by a dictionary since those are well suited to mapping names to values/objects. On a class, (or class instance), you can access the dictionary via ClassName.__dict__ or instance.__dict__ and you can do the same with a module: module.__dict__ if module is imported.

At a generic/language agnostic level, I think of a namespace as a way to bind a group of related names (and their associated values) together and to prevent those names from getting mixed up with similar names in a different namespace. e.g. I can have foo.something and bar.something. Both names are something, but they live in different namespaces (foo and bar respectively) so we can tell them apart.


'I'm assuming the namespace is the allotted place in memory in which the name is to be stored.' yep.


Need Your Help

Java Button Functions

java swing user-interface button

This is my first time designing a GUI and I believe this is probably a simple question. I am trying to get the program to close when the cancel button is pressed using System.exit(0) but I am not s...

Is there anything like Eclipse Perspective in Visual Studio?

visual-studio eclipse perspectives

I was wondering if there's anything like Eclipse Perspectives in Visual Studio (2008 or 2010).