How to Teach Python "Variables"

Robin Kåveland Hansen kaaveland at gmail.com
Tue Nov 27 13:58:25 EST 2007


Aurélien Campéas <spamless.aurelien.campeas at free.fr> wrote (Sun, 25 Nov
2007 20:09:59 +0100):

> none a écrit :
> 
> That's something I've often heard and I don't get it. Somehow I don't 
> understand how C variables are not like python bindings (the differences 
> being that C variables are statically typed and completely disappear at 
> run-time; are these differences important enough to warrant such a shift 
> in terminology ? (yes there are some other differences, but then the 
> question is asked in a context of pedagogy, where the audience is 
> introduced to the basics))
> 
> I mean : aren't C variables also bindings from names to objects ? Or what ?

This is actually something that comes up a lot on #python on freenode irc.
Usually we can explain it by calling things nametags that get bound to
objects and such, but sometimes it help to show people globals() and
locals() - that you can really just keep track of names with a dictionary
(And this is indeed, as I understand it, what's actually being done).
There's nothing that stops several names from being bound to the same
object, but names need to be unique. It's quite a good illustration, I
think. It certainly helped me. Oh, and there's the thing about people
calling them references, which isn't really accurate in the C++/Java or
pointer way. 

Considering a C++/Java reference can actually change what it, in that if
you send a reference to a variable into a function/method, you can risk
that your own "copy" will be "pointing" (For a lack of better words) to a
different object. This won't happen in Python. Mutable objects make it
seem very similar though.

-- 
regards, 
Robin



More information about the Python-list mailing list