A question on modification of a list via a function invocation

Steve D'Aprano steve+python at pearwood.info
Wed Aug 16 21:40:33 EDT 2017


On Thu, 17 Aug 2017 10:14 am, Ned Batchelder wrote:


> the name/value data model of
> Python is not some trivial detail that we could change to match some
> other language: it's a fundamental part of what makes Python what it is.

It is also an evaluation model which matches nearly all of the most popular
languages of the last two decades, including Java[1], Ruby, (I think) PHP, and
Javascript.


> For some reason, students have been taught that things can be either
> call-by-reference or call-by-value. But those are not the only two
> possibilities, and neither completely describes how Python works.
> 
> Learn Python for what it is.
> 
> --Ned.

Indeed. The insistence on this false dichotomy of call by value versus by
reference puzzles me, since alternatives go back to one of earliest, and most
influential, programming languages: Lisp. Algol, another early and hugely
influential language, used call by name.

Its like there was an entire generation of cooks and chefs who insisted that
there are only two ways of cooking food: steaming, and barbecuing, and came up
with elaborate rationalisations for why frying, roasting, boiling etc are
actually one of the two.




[1] Despite the obnoxious insistence of Java experts that it is "pass by value",
where the value they are talking about is not the actual value, but some
hidden, implementation-dependent pointer or reference to the value:

http://javadude.com/articles/passbyvalue.htm

In the words of the Effbot Fredrik 

    well, I guess you can, in theory, value an artificial number 
    assigned to an object as much as the object itself.

    "Joe, I think our son might be lost in the woods"
    "Don't worry, I have his social security number"


http://www.effbot.org/zone/call-by-object.htm


It absolutely astonishes me how the Java community abuse the term "call by
value" in this way, despite the confusion it causes, and despite knowing the
correct answer. Scott Stanchfield actually does know what is going on. Despite
the inflammatory title of his post, and the outright false comment right at the
start:

    "Java is strictly pass-by-value, exactly as in C."

deep in the article linked above, he comes clean and writes:

    "A correct statement would be /Object references are passed by value/."

[emphasis /.../ in original]

But the objects themselves are not, as would be required by pass-by-value
semantics. So he knows that the values, the objects themselves, are not copied
when you pass them to a function. Only the invisible references to the objects
are copied, but they are not the values anyone cares about. The example he
shows has precisely the same behaviour as the equivalent Python code.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list