How to change string or number passed as argument?

Discussion in 'Python' started by Peng Yu, Sep 20, 2009.

  1. Peng Yu

    Peng Yu Guest

    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
     
    Peng Yu, Sep 20, 2009
    #1
    1. Advertising

  2. Peng Yu

    Tim Chase Guest

    > 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
     
    Tim Chase, Sep 20, 2009
    #2
    1. Advertising

  3. 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
     
    Hendrik van Rooyen, Sep 20, 2009
    #3
  4. Peng Yu

    Simon Forman Guest

    On Sep 19, 9:59 pm, Peng Yu <> 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
     
    Simon Forman, Sep 21, 2009
    #4
  5. En Sat, 19 Sep 2009 22:59:21 -0300, Peng Yu <> 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
     
    Gabriel Genellina, Sep 22, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    538
    Joseph Byrns
    Apr 15, 2005
  2. Fong
    Replies:
    3
    Views:
    1,230
  3. Anand
    Replies:
    2
    Views:
    934
    Anand
    Sep 11, 2003
  4. Allen
    Replies:
    6
    Views:
    328
    Default User
    Feb 6, 2007
  5. Adam
    Replies:
    30
    Views:
    939
    Harald van Dijk
    Nov 11, 2008
Loading...

Share This Page