Newbie question: Returning mutable vs. immutable

Discussion in 'Python' started by pugnatio2@yahoo.com, Apr 15, 2004.

  1. Guest

    Hi, Does anyone have an opinion as to whether functions/methods should
    return mutable or immutable objects? In some circumstances, I could
    see that mutable objects might be more convenient, but if the function
    is to be reused, it might be a good idea to return only immutables.

    Or is this question moot, since the function returns control to the
    caller and is out of the picture at that point?

    Thanks,

    --p
     
    , Apr 15, 2004
    #1
    1. Advertising

  2. Roy Smith Guest

    In article <>,
    wrote:

    > Hi, Does anyone have an opinion as to whether functions/methods should
    > return mutable or immutable objects? In some circumstances, I could
    > see that mutable objects might be more convenient, but if the function
    > is to be reused, it might be a good idea to return only immutables.
    >
    > Or is this question moot, since the function returns control to the
    > caller and is out of the picture at that point?
    >
    > Thanks,
    >
    > --p


    I don't think there's any general rule for this. Return what makes
    sense to return and don't worry about the mutability unless there's some
    specific reason it needs to be mutable (i.e. you're going to change it)
    or immutable (i.e. you're going to use it as a dictionary key).
     
    Roy Smith, Apr 15, 2004
    #2
    1. Advertising

  3. John Roth Guest

    <> wrote in message
    news:...
    > Hi, Does anyone have an opinion as to whether functions/methods should
    > return mutable or immutable objects? In some circumstances, I could
    > see that mutable objects might be more convenient, but if the function
    > is to be reused, it might be a good idea to return only immutables.
    >
    > Or is this question moot, since the function returns control to the
    > caller and is out of the picture at that point?
    >
    > Thanks,
    >
    > --p


    I'm not sure I understand the question. The entire reason (well, not
    the *entire* reason, but close enough on the novice level) you write
    functions is to use them again and again and again. So your function
    should return whatever it needs to.

    In thinking about this, it seems there is really only one place where
    you would have a choice of mutable or immutable: a list or a tuple.
    Think of a tuple as a packaging method for a number of distinct
    values that you want to return. For example, if I say:

    return spam, eggs, juice

    the compiler builds a tuple. If I wanted to return a list, I'd have
    to say:

    return [spam, eggs, juice]

    John Roth
     
    John Roth, Apr 15, 2004
    #3
    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. hiwa
    Replies:
    4
    Views:
    527
    Oliver Wong
    Mar 8, 2006
  2. Replies:
    5
    Views:
    430
  3. Bernard Lim

    Immutable and Mutable Types

    Bernard Lim, Mar 17, 2008, in forum: Python
    Replies:
    16
    Views:
    681
    Terry Reedy
    Mar 19, 2008
  4. Einar Høst

    [Newbie] Immutable objects

    Einar Høst, Mar 9, 2006, in forum: Ruby
    Replies:
    9
    Views:
    202
    Robert Klemme
    Mar 10, 2006
  5. Deb Wyatt

    immutable vs mutable

    Deb Wyatt, Jun 3, 2014, in forum: Python
    Replies:
    13
    Views:
    84
    Mark H Harris
    Jun 4, 2014
Loading...

Share This Page