var or inout parm?

sturlamolden sturlamolden at yahoo.no
Fri Dec 12 07:34:48 EST 2008


On Dec 7, 9:54 am, m... at pixar.com wrote:

> How can I make a "var" parm, where the called function can modify
> the value of the parameter in the caller?
>
> def f(x):
>     x = x + 1

Short ansver:

You can modify function parameters if they are mutable. If they are
immutable any attempt to modify the parameter will trigger a copy.

You cannot modify parameters by rebinding. x = x + 1 is a rebinding. x
+= 1 is not.

If you need to modify immutable parameters or do the modification by
rebinding, pass the variable back. Python allows multiple return
values.



















More information about the Python-list mailing list