How to change string or number passed as argument?

P

Peng Yu

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
 
T

Tim Chase

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.

The most common way is to simply return the altered string if you
need it:

def my_func(some_string):
result = do_stuff(...)
some_string = mutate(some_string)
return result, some_string

result, value = my_func(value)

This gives the flexibility for the caller to decide whether they
want to allow the function to mutate the parameter or not.


You can also use a mutable argument:

def my_func(lst):
lst[0] = mutate(lst[0])
return do_stuff(...)
s = ["hello"]
result = my_func(s)
print s[0]

but this is horribly hackish.

In general, mutating arguments is frowned upon because it leads
to unexpected consequences. Just like I don't expect sin(x) or
cos(x) to go changing my input value, python functions should
behave similarly.

-tkc
 
H

Hendrik van Rooyen

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.

You can just ignore the immutability.
Nothing stops you doing something like this;

def reader(port,buffer):
buffer += port.read()
return buffer

and calling it repetitively until buffer is as long as you want it.

- Hendrik
 
S

Simon Forman

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
 
G

Gabriel Genellina

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top