passing by refference

Chris Liechti cliechti at gmx.net
Tue May 13 15:03:59 EDT 2003


Joshua Marshall <joshway_without_spam at myway.com> wrote in 
news:b9r933$8q0$1 at ginger.mathworks.com:

> Mel Wilson <mwilson at the-wire.com> wrote:
>> In article <mailman.1052835140.9801.python-list at python.org>,
>> Daan Hoogland <hoogland at astron.nl> wrote:
>>>is passing by reffence possible in python?
> 
>>    Everything in Python is passed by reference.
> ...
> 
> This is incorrect terminology.  Python has call-by-value semantics,
> not call-by-reference (it is not possible to pass a reference to a
> variable into a function).

uhm...

>>> def f(x):
... 	print id(x)
... 	
>>> a=5
>>> id(a)
3328264
>>> f(a)
3328264
>>>  

as you can clearly see, the reference to the same object is passed into the 
function. but its rebound to a new name in the function. assiging to that 
name just rebinds a new reference to that name, which has no influence on 
the callers namespace.

for out-parameters its an usual practice to use a list:

>>> def f(x):
...   x[0] = 2
...   
>>> a = [1]
>>> f(a)
>>> a
[2]
>>> 

altough out-parameters are seldom used in python as we can have multimple 
return values, as Mel Wilson and others pointed out.

chris
-- 
Chris <cliechti at gmx.net>





More information about the Python-list mailing list