Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie question: Returning mutable vs. immutable

Reply
Thread Tools

Newbie question: Returning mutable vs. immutable

 
 
pugnatio2@yahoo.com
Guest
Posts: n/a
 
      04-15-2004
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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      04-15-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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).
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      04-15-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
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
Immutable and Mutable Types Bernard Lim Python 16 03-19-2008 04:47 AM
behavior difference for mutable and immutable variable in function definition jianbing.chen@gmail.com Python 5 05-05-2007 08:05 PM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
[Newbie] Immutable objects Einar HÝst Ruby 9 03-10-2006 10:49 AM
Why mutable/immutable can't be valid class qualifier? hiwa Java 4 03-08-2006 04:46 PM



Advertisments