On 12/09/2013 05:46 AM, Varuna Seneviratna wrote:

Let’s begin with some definitions.

A *namespace* is a mapping from names to objects. Most namespaces are
currently implemented as Python dictionaries, but that’s normally not
noticeable in any way (except for performance), and it may change in the
future. Examples of namespaces are: the set of built-in names (containing
functions such as abs()<http://docs.python.org/2/library/functions.html#abs>,
and built-in exception names); the global names in a module; and the local
names in a function invocation. In a sense the set of attributes of an
object also form a namespace. The important thing to know about namespaces
is that there is absolutely no relation between names in different
namespaces; for instance, two different modules may both define a function
maximize without confusion — users of the modules must prefix it with the
module name.

    The above paragraph was extracted from the description about namespaces
from the Python tutorial(Python Scopes and
Namespaces<http://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces>).I
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.For a example within the 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.Am I right?. But what is meant by "A
*namespace* is a mapping from names to objects"
Thanks Varuna

A namespace (yes, the term is weird, even wrong, but this applies to nearly all terms in programming) is a set of symbols. A symbol in programming is: * like any symbol a relation between a form (the id, or name) and a meaning (or "semantics" if you want to look pedantic) * plus, in programming, nearly all symbols relate with elements of the program, usually called data, value, or object.

        # definition of a symbol
        board_size = 8

After that definition:

        id: "board_size"      <--->     meaning: <size of a check board>
        (in code, maybe memory)                 (in our minds)
            ^
            |
            v
        value: 8 (type `int`)
        (in code and computer memory)

A namespace is a set of such symbols, whatever their value. Since an id (identifier) by definition identifies a symbol, it can only be unique. In python and many other languages, there are such kinds of namespace: * function scope: the local symbols of a function (parameters, constants, variables, other function or types defined there...) * global space: should be used to define the top-level elements of the world (the system you describe), else avoided * composite elements: objects usually hold attributes which are sub-symbols, thus they are namespaces as well. For instance, a position could hold (x, y) or (radius, angle).

Some languages make separate namespaces for different kinds of elements. In particular, functions or types may be stored apart; this is not the case in Python.

You will also find the term "symbol table", which would be, and in fact is, a far better term for "namespace", but used in compilation. In addition to name and value, a compiler stores other information about symbols found in a program it is decoding.

Denis
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to