Teaching python (programming) to children

Danyel Fisher danyelf at acm.org
Tue Nov 6 19:12:06 CET 2001


[global example deleted]

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

At many schools, Scheme is taught as a first language. It's a lispy language
with nested scopes, and students have _absolutely_ no problem figuring this
out. One way to do it is to start off teaching functions without (gasp!)
global scope. "A function is something that returns stuff." Students who
really need to return multiple things get tuples, so it isn't a big deal.

At some point, you get to say "but there's a slightly easier way,
sometimes." When teaching lisp, the easier way is through the notion of
"environments" and nested scope. In python, we call it a the "globals
list"--a list of variables that you can get at without sending them into a
function.

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

Nope. Teacher goes through the short song and dance about local variables
and ignores globals until the students are already comfortable with locals.

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

Again, the students have good "function" habits. They'll only set the global
"flag" variable when they need to.

You seem to work under the (rather odd) assumption that teaching works by
standing in front of a bunch of students, yelling at them, and then letting
them run off into the world to be confused. Some of us who are teachers
prefer to give students a series of increasingly-sophisticated assignments,
any of which can be solved by the material we've covered so far in class,
and each of which illustrates an interesting principle of the subject
matter.

Kudos, for example, to Kojo Duncan's teacher, who asked a striaghtforward
question ("write a program that lists all prime numbers less than a prime
number a user inputs.") The solution to it requires only traditioanl
functions, few namespaces, and yet is fairly easy.

For students who know prime numbers--not our target audience of 10 year
olds--this is a great assignment.

Danyel





More information about the Python-list mailing list