[Tutor] beginning to code

Steve D'Aprano steve+python at pearwood.info
Fri Sep 22 07:30:23 EDT 2017


On Fri, 22 Sep 2017 05:01 pm, Bill wrote:

> Steve D'Aprano wrote:
>> On Fri, 22 Sep 2017 02:57 pm, Bill wrote:
>>
>>> I find Python to be more more
>>> like Java, with regard to "passing objects by reference".
>> Which is not a surprise, since both Python and Java use the same value
>> passing style: pass by object reference, or pass by sharing if you prefer.
>>
>> Java people don't call it that. They call it pass by value, and categorically
>> deny that it is pass by reference. (They're right about the second point.)
> 
> I figure that, internally, an address, a pointer, is being passed by
> value to implement pass by reference.  Why do you say "they are right"
> above? Are you saying it's not pass by reference?

Are you suggested that the Java people are wrong to say it isn't?

Last time I suggested that the Java community had made a mistake in this regard,
I got blasted by certain folks here for insulting the intelligence of compiler
experts.

But yes, I agree with the Java community that what they do is NOT pass by
reference. It clearly is not: the canonical test for pass by reference is
whether you can write a "swap" procedure which swaps two variables:

a = 1
b = 2
swap(a, b)
assert a == 2 and b == 1

A general swap procedure cannot be written in Python or Java. (We don't need
one, in Python, because we have alternatives which are arguably better, but
that's not the point.)

So I am in 100% agreement with the Java people when they say what they do (and
what Python does) is not pass by reference.


> What really screws the terminology up is that it's not parameters that
> are being passed, it's arguments! %-)

I don't believe that I mentioned parameters or arguments, so that's a red
herring.

The terminology is fine, if we don't insist on ignoring it. There are many more
kinds of passing semantics than just by value and by reference, and the
confusion only occurs when people insist on taking the round peg of a
language's actual behaviour, and hammering it in by brute-force into the square
hole of "pass by value" or "pass by reference". Quoting Wikipedia:

    In some cases, the term "call by value" is problematic, as
    the value which is passed is not the value of the variable as 
    understood by the ordinary meaning of value, but an 
    implementation-specific reference to the value.


Some examples:

- call by sharing
- call by need
- call by name
- call by copy-restore ("copy in, copy out")

etc.


https://en.wikipedia.org/wiki/Evaluation_strategy


-- 
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