Re: How to change string or number passed as argument?

2009-09-21 Thread Gabriel Genellina

En Sat, 19 Sep 2009 22:59:21 -0300, Peng Yu pengyu...@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

--
http://mail.python.org/mailman/listinfo/python-list


Re: How to change string or number passed as argument?

2009-09-20 Thread Hendrik van Rooyen
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change string or number passed as argument?

2009-09-20 Thread Simon Forman
On Sep 19, 9:59 pm, Peng Yu pengyu...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


How to change string or number passed as argument?

2009-09-19 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change string or number passed as argument?

2009-09-19 Thread 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



--
http://mail.python.org/mailman/listinfo/python-list