How to change string or number passed as argument?

Simon Forman sajmikins at gmail.com
Mon Sep 21 06:44:17 CEST 2009


On Sep 19, 9:59 pm, Peng Yu <pengyu... at gmail.com> wrote:
> Hi,
>
> I know that strings or numbers are immutable when they passed as
> arguments to functions. But there are cases that I may want to change
> them in a function and propagate the effects outside the function. I
> could wrap them in a class, which I feel a little bit tedious. I am
> wondering what is the common practice for this problem.
>
> Regards,
> Peng

Python strings and numbers are always immutable, not just when passed
as arguments.

"propagate the effects outside the function" is a little vague.

You can return new data objects (like str.lower() etc.. do) or you can
wrap them in a namespace (a dict or class instance) or you can pass a
list object that contains the string or int or whatever, and your
functions can replace the values in the dict/instance/list.

HTH,
~Simon



More information about the Python-list mailing list