How to Teach Python "Variables"

Neil Cerutti horpner at yahoo.com
Tue Nov 27 13:26:43 CET 2007


On 2007-11-26, davisn90210 at gmail.com <davisn90210 at gmail.com> wrote:
> Hrvoje Niksic wrote:
>> greg <greg at cosc.canterbury.ac.nz> writes:
>>
>> > none wrote:
>> >> IIRC, I once saw an explanation how Python doesn't have
>> >> "variables" in the sense that, say, C does, and instead has
>> >> bindings from names to objects.
>
> IMHO, this is nonsense.  All that variables are (in any
> language) are "bindings" for names. Pretending python does
> anything novel with regard to "variables" is confusing and,
> ultimately, simply results in a redefinition of terms
> programmers are already familiar with. 

Someone who says Python identifiers aren't variables, as in C, is
thinking of C variables, which behave quite differently than
Python identifiers.

> I mean, it's kind of like saying my computer is not a computer
> but is actually a device that follows input directions.  Of
> course that description may be true (and I may even use it to
> explain to students *what* a computer is), but it is no less a
> computer for it.

Your analogy is begging the question. Noone is claiming that a
variable is not a variable. Python identifiers are variables in a
broad sense. But they aren't like C variables.

>> > If you're talking to C programmers, just tell them that
>> > Python variables always contain pointers. That should give
>> > them the right mental model to build on.
>>
>> That is a convenient shortcut when it works, but in my
>> experience it tends to confuse the issue.  The reason is that
>> one of the main uses of pointers in C is implementing
>> pass-by-reference.  A C programmer told that Python variables
>> internally hold pointers expects this code:
>
> I think most C programmers are smart enough to figure out the
> supposed differences, with only a little additional explanation
> on the part of the instructor.  Python's "variable model" is
> practically identical to that of Java, after all, and I don't
> recall any discussion of cataclysmic proportions over the
> differences between C "pointers" and Java "references".  There
> are "differences" (or more accurately "points of emphasis"),
> but of the sort that take a paragraph or two of
> explanation/clarification -- not a completely new model.

There are more differences than similarities. Pointers are are a
low-level mechanism suitable for many purposes, referencing
values amongst them. Python identifiers are a high-level
machanism, suitable for only one purpose.

-- 
Neil Cerutti



More information about the Python-list mailing list