Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to change string or number passed as argument?

Reply
Thread Tools

How to change string or number passed as argument?

 
 
Peng Yu
Guest
Posts: n/a
 
      09-20-2009
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
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      09-20-2009
> 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



 
Reply With Quote
 
 
 
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      09-20-2009
On Sunday 20 September 2009 03:59:21 Peng Yu wrote:

> 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

 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      09-21-2009
On Sep 19, 9:59*pm, Peng Yu <(E-Mail Removed)> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      09-22-2009
En Sat, 19 Sep 2009 22:59:21 -0300, Peng Yu <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM
HELP - how to convert a string representation of a number to a number ? cpptutor2000@yahoo.com C++ 5 05-21-2005 05:56 PM
Python C extension: Value different if passed as list than if passed as number Anand Python 2 09-11-2003 01:02 AM



Advertisments