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

2013-12-09 Thread Varuna Seneviratna

 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
Namespaceshttp://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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2013-12-09 Thread Amit Saha
On Mon, Dec 9, 2013 at 2:46 PM, Varuna Seneviratna
varunasenevira...@gmail.com 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(), 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).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?.

You can name anything else as abs - a function, for example. But that
will override the built-in abs() function. For example:

 abs(1) # built-in abs
1
 def abs(num):
... print('In my abs')
...
...
 abs(1)
In my abs


Similarly:

 abs = 1
 abs()
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'int' object is not callable


But what is meant by A namespace is a mapping from
 names to objects

I don't think I can explain this clearly enough to help elucidate the
literal meaning, so I hope somebody else will.

Best,
Amit.

-- 
http://echorand.me
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2013-12-09 Thread Steven D'Aprano
On Mon, Dec 09, 2013 at 10:16:30AM +0530, Varuna Seneviratna wrote:

 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?. 

Mostly right. Only one function can be called abs in the built-in 
names at the one time. You can (but you shouldn't!) change that 
function, so it is not necessarily the abs function that you expect. 
Nevertheless, the basic idea is okay.


 But what is meant by A
 *namespace* is a mapping from names to objects

When Python sees the code abs(x), it needs to know which function to 
call. The name abs alone isn't a function. There needs to be some sort 
of connection, some link, between the name abs and the function which 
returns the absolute value. That is what is called a mapping.

Likewise, the name x isn't a number. There needs to be a link, a 
mapping, between the name x and some value like 23 or -17.

Both functions and data values (like numbers and strings) are objects, 
so instead of saying a mapping between names and functions or numbers 
or strings or lists or ... we just say between names and objects.

When Python sees a name, say, spam, it first looks in the *local* 
namespace for a variable called spam. If there is no such variable, it 
then looks in the *global* namespace, and if still not found it looks in 
the built-in namespace. Finally if still not found it raises NameError.

(I have simplified a little bit, but not too much.)

Does this help?



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


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

2013-12-09 Thread spir

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
Namespaceshttp://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


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

2013-12-09 Thread Alan Gauld

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

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


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

2013-12-09 Thread Marc Tompkins
On Sun, Dec 8, 2013 at 8:46 PM, Varuna Seneviratna 
varunasenevira...@gmail.com wrote:

 But what is meant by A *namespace* is a mapping from names to objects

Steven touched on this, but I'd like to emphasize: in Python, EVERYTHING is
an object - variables, functions, integers, strings, you name it.  (Even
names and namespaces, but that might be getting a little TOO meta.)
When I first came to Python, after years of languages where some things are
objects and other things aren't, this concept gave me a little trouble;
once I got my head around it, namespaces made sense too.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor