IsString

Steven D'Aprano steve at REMOVEMEcyber.com.au
Wed Dec 14 03:24:33 EST 2005


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


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



-- 
Steven.




More information about the Python-list mailing list