cannot pass a variable from a function

Miki Tebeka miki.tebeka at
Thu Jun 17 08:54:28 CEST 2004

Hello Doug,

> In other language
> subroutine foo(b,c)
>     c=b*1000
> return
> call foo(q,r)
> where q and r are defines and same type as b,c as  function
> How do I do this in python.  I need to perform operations on a variable and
> pass the new variable to the program.
I think you mean "call by reference". In this case you can only modify
"compound" types (there is a better word for this) such as lists, has
tables ...
If you do:
def f(l):

a = []
f(a) # a -> [1]

However you can't do that to "simple" types such as int, long ...
def f(x):
    x += 2
a = 1
f(a) # a -> 1

IMO this is not a problem since in Python you can returns multiple
values and less side effects = less bugs.

If you *must* do this you can wrap your variables:
def f(x):
    x.a += 2

class P:
p = P()
p.a = 1
f(p) # p.a -> 3


Miki Tebeka <miki.tebeka at>
The only difference between children and adults is the price of the toys.

More information about the Python-list mailing list