Teaching python (programming) to children

Hung Jung Lu hungjunglu at yahoo.com
Tue Nov 6 11:38:53 EST 2001


How soon does a beginner run into the following problem? (see also the
"How much is set in stone?" thread)

#------------------
flag = 0

def f(x):
    flag = 1
    return 2*x

y = f(3)
print y, flag
#------------------
output: 
6 0

I'd say, most run into this problem soon after they learn to use
functions. It does not matter whether they have seen any other
programming language before or not.

Some have said "don't teach them about 'from xyz import *'". Should we
also not teach beginners about using functions? Python's namespace
feature is such an integral part of the language, that you can't
really neglect it.

Sequence of events:

(1) Teacher goes through the whole song and dance about global, local
namespace. How assignment actually means name binding, etc. etc. It is
fun to do it once, it is fun to do it twice, but after doing it a few
dozen times, teacher gets exhausted, and starts to cast doubt: "...
maybe Python is not such a trivial language, after all."

(2) After being shocked by the above example, students start to put
'global' statements in every single function for every single
variable. They don't understand what is going on, they just don't want
to be bitten by the same bug, again.

(3) After a long (and it can be very long) time stepping on other
namespace landmines, a few students finally come to realize how
namespaces and dictionaries really work. They become teachers, and go
to step (1).

(4) The above is repeated for thousands and thousands of average-level
Python users.

So, given a language that does not have to deal with global/local
namespace pitfalls, and a language that has to deal with it, which one
would you choose as the first language to teach?

Hung Jung



More information about the Python-list mailing list