IsString

Mike Meyer mwm at mired.org
Wed Dec 14 12:51:43 EST 2005


Steven D'Aprano <steve at REMOVEMEcyber.com.au> writes:
> Mike Meyer wrote:
>>>So Python behaves demonstrably different from BOTH call by value and call
>>>by reference. Consequently, it is neither of them.
>> Right. *Python* behaves differently. That's not the same thing as
>> Python's calling behaving differently. If you choose objects that are
>> behave the same in both languages, Python behaves *exactly* like
>> call-by-reference.
> This would possibly be a valid argument if Python *only* had such
> objects that behaved the same in call by reference languages, but it
> doesn't. It also has objects which behave differently. Just because
> Python can, in certain restricted circumstances, act like call by
> reference doesn't mean it is call by reference, because there are a
> whole swag of circumstances where Python _can't_ act like call by
> reference.

You've got this exactly backwards.  Every object in Python behaves the
same way as similar objects in call-by-reference lannguages. Those
languages have objects which have no analogue in Python. If you use
one of them as your example of what "call-by-reference" does, it will
of course have behavior that can't be reproduced in Python. That isn't
because Python isn't call-by-reference; it's because Python doesn't
have objects that behave like the one you're using for an example.

>> You can show the same difference in behavior between Python and C (for
>> example) without using a function call.
>> Here's C:
>> #include <assert.h>
>> main() {
>>   int i, *ref ;
>>   i = 1 ;
>>   ref = &i ;	/* Save identity of i */
>>   i = 2 ;
>>   assert(ref == &i) ;
>> }
>> This runs just fine; i is the same object throughout the program.
>> On the other hand, the equivalent Python:
>>
>>>>>i = 1
>>>>>ref = id(i)	# Save the identity of i
>>>>>i = 2
>>>>>assert ref == id(i)
>>>>
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> AssertionError
>> Blows up - i is no longer the same object.
> Your argument seems to me like some sort of reverse bizarro "looks
> like a duck" argument: since Python _doesn't_ behave like call by
> reference, it must _be_ call by reference.

You're right in that this doesn't demonstrate that Python has
call by referece. What it shows is that your example of why Python
isn't call by referencde is invalid: your example depends on an object
behavior that doesn't exist in Python, no matter what calling
convention it uses.

>> Python does call by reference, which means that it passes pointers to
>> objects by value.
> You are confusing the underlying implementation in C with Python the
> language. I couldn't care less whether the underlying implementation
> allows call by reference or not. That is simply irrelevent to the
> question of whether Python does call by reference.
> Python doesn't have a pointer object type. There is an implementation
> of Python written in pure Python. That demonstrates that there is at
> least one implementation of Python where it is demonstrably _not_ true
> that Python "passes pointers to objects by value".
> What do you think that does to your argument?

I think you're right in the first paragraph - that it's discussing the
implementation, and is thus totally irrelevant to the question of
whether or not python does call by reference.

To restate the first paragraph: Python's calling semantics are a
proper subset of call-by-reference. If the object passed has the same
behavior as it does in a call-by-reference lanuage, then the behavior
of the argument is the same as it is in the call-by-reference
language. You can argue that one of the missing things is crucial to
the definition of call by reference, in which case we disagree about
the meaning of call by reference, and we can stop there. Or you can
argue that there is some object in Python that behaves the same way in
both Python and a call by reference that does not behave the same way
when passed as an argument. I'm still waiting for an example of that.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list