Teaching python (programming) to children

Hung Jung Lu hungjunglu at yahoo.com
Wed Nov 7 03:30:13 EST 2001


huaiyu at gauss.almadan.ibm.com (Huaiyu Zhu) wrote in message news:<slrn9uh4ru.1hg.huaiyu at gauss.almadan.ibm.com>...
Huaiyu Zhu wrote:

> You are confused about the cause of this problem.  

I don't think so.

> It is has nothing to do with namespace.  

Now you are confused. You think you can do Python without namespaces.

  statement        modification on the globals() namespace dictionary
----------------+------------------------------------------------------
>>> a = []      | (1) yes, entry with key 'a' inserted
>>> x = a       | (2) yes, entry with key 'x' inserted
>>> x.append(1) | (3) no, no action on globals() namespace dictionary
>>> print a     |
[1]             |
>>> x = []      | (4) yes, value of the entry with key 'x' updated
>>> print a     |
[1]             |

The difference in (3) and (4) is simply whether action has been
performed on the namespace dictionary, regarding the entry with key
'x'.

(Forgive me for not entering into issues on pointers and objects in
the heap, I don't want to open another can of worms.)

It is perhaps more instructive to see this:

globals().update({'x': 3})
y = x+1
print x, y

And from there you can understand that there is a namespace operation
whenever you talk about binding. The difference between your example
and my example is simply that your 'x' lives in the globals namespace,
mine lives in the locals namespace, which is not the point. The point
is one has to understand that Python's assignments, Python's 'global'
statement, Python's 'from xyz import *' statement, etc. are merely
syntactic sugar, and that the actual operations are performed on the
namespace dictionaries. And remember, we are not talking about people
here not understanding Python, we are talking about how it is hard to
explain to beginners these things.

Experienced Python programmers always keep the three Python namespaces
in mind. Matter of fact, many good Python programmers, including those
that made Zope and hired Guido [:)], do tweak the built-in namespace
once in a while. I guarantee you, once you have the namespace
dictionaries picture inside your head, you won't need to use terms
like "Python distinguishes between mutation and name-binding" and you
won't run into namespace problems again. Once you understand the
namespace dictionary mechanism, you'll see that all the three examples
('from xyz import *', 'global', and this one) I have mentioned all
come from the lack of understanding of the namespace mechanism.
Without understanding the namespace mechanism, you think they are
three unrelated problems. (Missing the forest because of the trees, as
they say in English, or, like blindmen touching the elephant, as they
say in Chinese.) Once understood the namespace mechanism, you won't
have any of these problems. Frankly, I don't think a person has
understood Python until they have understood the namespace mechanism.
My advise is: get to see the whole forest, or the whole elephant, or
the whole Python. :)

However, that's not a task for beginners. Me having to spend 4
postings on this thread should tell you something: many people don't
see the whole Python.

>feeling-exhausted-explaining-this-many-times-ly yr's

Believe me, I am more exhausted than you are. :)

Hung Jung



More information about the Python-list mailing list