function parameter

Delaney, Timothy tdelaney at avaya.com
Mon Jan 29 19:14:40 EST 2001


Python does neither call by reference nor call by value.

What python really does is pass reference by value. i.e. a reference to an
object is passed, and you can then modify the object that the reference is
referring to (if it is mutable), but you cannot modify what the reference is
referring to and have it propagate out of the namespace.

See the following examples:

>>> def f1 (p):
...     p = 2
...
>>> def f2 (p):
...     p[0] = 2
...
>>> a = (1,)
>>> b = [1]
>>>
>>> f1(a)
>>> a
(1,)
>>> f1(b)
>>> b
[1]
>>> f2(a)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in f2
TypeError: object doesn't support item assignment
>>> f2(b)
>>> b
[2]
>>>

Tim Delaney
Avaya Australia
+61 2 9352 9079

> -----Original Message-----
> From: Hwanjo Yu [mailto:hwanjoyu at uiuc.edu]
> Sent: Tuesday, 30 January 2001 10:41 AM
> To: python-list at python.org
> Subject: Q: function parameter
> 
> 
> Hi,
> It seems that the python's function parameters are passed 
> call-by-value as a
> default.
> How to make it call-by-reference, so it allows the parameters 
> to be changed
> after calling a function ?
> Thanks in advance.
> 
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list