Mutability of function arguments?

Mark Tolonen nospam at nospam.com
Wed Dec 7 20:50:19 EST 2005


"ex_ottoyuhr" <ex_ottoyuhr at hotmail.com> wrote in message 
news:1134001014.716406.10800 at z14g2000cwz.googlegroups.com...
> I'm trying to create a function that can take arguments, say, foo and
> bar, and modify the original copies of foo and bar as well as its local
> versions -- the equivalent of C++ funct(&foo, &bar).
>
> I've looked around on this newsgroup and elsewhere, and I gather that
> this is a very common concern in Python, but one which is ordinarily
> answered with "No, you can't. Neat, huh?" A few websites, newsgroup
> posts, etc. have recommended that one ask for a more "Pythonic" way of
> doing things; so, is there one, or at least one that doesn't involve
> using objects as wrappers for mutable arguments?
>
> And, indeed, would that approach work? Would declaring:
>
> class FooWrapper :
>    __init__(fooToLoad) :
>        self.foo = fooToLoad
>
> mean that I could now declare a FooWrapper holding a foo, pass the
> FooWrapper to a function, and have the function conclude with the foo
> within the FooWrapper now modified?
>
> Thanks in advance for everyone's time; I hope I'm comprehensible.
>

Python isn't C++ and there is no need to return multiple values by modifying 
function parameters:

>>> def funct(a,b):
...  return a+1,b+1
...
>>> foo,bar=1,2
>>> print foo,bar
2 3
>>> foo,bar=funct(foo,bar)
>>> print foo,bar
3 4
>>>

-Mark 





More information about the Python-list mailing list