Objects in Python

Jussi Piitulainen jpiitula at ling.helsinki.fi
Thu Aug 23 04:59:56 EDT 2012


Steven D'Aprano writes:
> On Wed, 22 Aug 2012 23:49:17 -0500, Evan Driscoll wrote:
> 
> > On 8/22/2012 18:58, Ben Finney wrote:
> >> You haven't discovered anything about types; what you have
> >> discovered is that Python name bindings are not variables.
> >> 
> >> In fact, Python doesn't have variables – not as C or Java
> >> programmers would understand the term. What it has instead are
> >> references to objects (with names as one kind of reference).
> > 
> > OK, I've seen this said a few times, and I have to ask: what do
> > you mean by this? I consider myself pretty decent at Python and
> > other languages, and I really don't get it.
> 
> I think the point that Ben would like to make is that while "name
> binding" is a specific kind of "variable", the word "variable" comes
> with too much baggage from the old-school C, Pascal etc. style of
> variables- are-named-memory-locations. Most of the time, the
> differences are unimportant, but when they are important, if your
> mental image is that Python "variables" (name bindings) are like C
> or Pascal "variables" (memory locations), you're going to get
> confused.

I don't get it either. To me the python-has-no-variables campaigners
seem confused. As far as I can see, Python can be seen in terms of
variables bound to (locations containing) values perfectly well, in a
way that should be quite familiar to people who come from Java (or
Lisp, Scheme like me).

It is very bad that people campaign here against the terminology that
is used in the official Python language reference and the Python
tutorial.

Best would be to simply explain how the variables, values, assignment,
and calls work in Python, the way the language reference and tutorial
actually do.

If the no-variables campaign is to continue, the language reference
and the tutorial should be changed to match. I would consider it a mad
move, but the current practice of badmouthing the official wording is
not healthy either.

> [...]
> > And many other languages have reference behavior and still call
> > their bindings "variables", e.g. Scheme.
> 
> Yes, well in my opinion, a great deal of the terminology in use is
> absolutely dire. E.g. both Ruby and Java have the exact same
> parameter binding strategy as Python, only the Ruby people call
> theirs "call by reference" and the Java people call theirs "call by
> value", *both of which are identical*, and NEITHER of which are the
> same thing that C and Pascal programmers will understand by call by
> value *or* call by reference.

That's indeed such a mess that those call-by-* terms may be now best
avoided. I would also avoid any distinction between an object and a
"reference" to an object, except as an implementation detail. It's not
helpful. It only leads to the confusion where it seems that Java (or
Python) does not actually have objects at all, only references to
objects, which in turn don't exist, so, er, what.

The swap function is helpful. Why doesn't it work? Because it assigns
to different variables that are local to the function. If I pass it a
list, why can it then swap the elements? Because that is the same
list, not a copy. Get used to it. Works for me.



More information about the Python-list mailing list