How to Teach Python "Variables"
davisn90210 at gmail.com
davisn90210 at gmail.com
Tue Nov 27 05:26:58 CET 2007
Chris Mellon wrote:
> On Nov 26, 2007 1:21 PM, 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. 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.
> Long real-life experience that people with previous programming
> experience, especially C programmers, have a great deal of trouble
> with this concept. Python "variables" do not work the way they do in
> other languages, where variables are compile-time bindings for memory
> locations. Even Java references aren't the same (there's no such thing
> as a null reference in Python, for example).
I myself learned C++ first, then transitioned to Java, then Python.
Once I got the hang of C++ pointers, I personally did not have any
significant difficulties with Java's references, and even less so with
Python's <whatever you want to call them>. Granted that's just my
experience, which doesn't mean others don't have that difficulty, but
that *is* my experience.
Also, to say that "there's no such thing as a null reference in
Python," is, IMHO, another example of nonesense. Null is a *concept*,
not a particular *value*, and simply means "there's no real value
here." The fact that Python's null is called "None" does not change
that. Furthermore, the fact that Python's designers were good enough
to create a "smarter" null than most languages have does not change
that. None is still a special value that is used to represent the
fact that there is "nothing here".
> > > > 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.
> C programmers, when told that Python works like this almost *always*
> get this wrong, because they want to pretend that Python is C-like
> pass by reference and it isn't.
Which is why you need the "points of emphasis"/"caveats". I never
claimed this is a slam-dunk.
> The very first thing they want to do is to get at "the pointer"
> underneath the object so they can simulate C style pass by reference
> with ints. It takes a lot of bandwidth (both mental and electronic)
> before they get that there is no underlying pointer and they can't do
Well, Java calls them "references" instead of "pointers", which may
not be a bad idea if only to emphasize that there are, in fact,
differences. Java seems to have gotten away with explaining these
differences in a pretty concise manner. I don't see any reason why
Python can't do likewise.
> The second thing they want to do is to intercept rebinding actions,
> like you might do in C++ with operator overloads. If you explain it to
> them in these terms, it is not clear (and will not be, until you use
> other terms) that rebinding a name (assignment) and mutating an object
> are fundamentally different operations.
I assume you are talking about overloading "operator =" in C++, but
that only works for non-pointers. How do you "intercept" rebinding of
a *pointer* in C/C++?
> I see these questions on a daily basis in #python and somewhat less
> frequently in c.l.p. Your assertion that C programmers will "just get
> it" if it's explained in those terms is simply not borne out by real
> world results.
Again, I never said C programmers will "just get it." That's why I
used the phrase "with only a little additional explanation." Maybe I
misinterpreted the original post, but my main point is simply that we
should call a spade a spade. Variables are variables whether they are
C pointers, Java references, or Python "name bindings. To say
otherwise just confuses the matter IMHO. There are differences, but
let's focus on the *differences* instead of re-naming things.
Sorry if I come off a little strong on a few points. Please keep in
mind that this is just my opinion of the matter.
More information about the Python-list