Python advocacy in scientific computation

Peter Maas peter.maas at somewhere.com
Mon Mar 6 16:22:06 EST 2006


Duncan Booth schrieb:
> sturlamolden wrote:
>> 1. Can python do "pass by reference"? Are datastructures represented by
>> references as in Java (I don't know yet).
>>
> Python only does "pass by reference", although it is more normally referred 
> to as "pass by object reference" to distinguish it from language where the 
> references refer to variables rather than objects.
> 
> What it doesn't do is let you rebind a variable in the caller's scope which 
> is what many people expect as a consequence of pass by reference. If you 
> pass an object to a function (and in Python *every* value is an object) 
> then when you mutate the object the changes are visible to everything else 
> using the same object. Of course, some objects aren't mutable so it isn't 
> that easy to tell that they are always passed by reference.

This is hard to understand for an outsider. If you pass an int, a float,
a string or any other "atomic" object to a function you have "pass by
value" semantics. If you put a compound object like a list or a dictionary
or any other object that acts as an editable data container you can return
modified *contents* (list elements etc.) to the caller, exactly like in
Java and different from C/C++.

Peter Maas, AAchen



More information about the Python-list mailing list