How to change string or number passed as argument?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Sep 21 21:40:30 EDT 2009
En Sat, 19 Sep 2009 22:59:21 -0300, Peng Yu <pengyu.ut at gmail.com> escribió:
> 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.
In addition to all previous responses: Sometimes, you have a function that
should return more than one piece of information. On other languages, you
have to choose *one* of them as *the* function return value, and the
others become out parameters. In Python you simply return all of them:
def decode_index(index):
"convert linear index into row, col coordinates"
return index // width, index % width # divmod would be better...
row, col = decode_index(index)
(Tecnically, you're still returning ONE object - a tuple. But since
packing and unpacking of values is done automatically, you may consider it
as returning multiple values at the same time).
--
Gabriel Genellina
More information about the Python-list
mailing list