Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Newbie question: Returning mutable vs. immutable (http://www.velocityreviews.com/forums/t329912-newbie-question-returning-mutable-vs-immutable.html)

pugnatio2@yahoo.com 04-15-2004 02:27 PM

Newbie question: Returning mutable vs. immutable
 
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

Roy Smith 04-15-2004 02:38 PM

Re: Newbie question: Returning mutable vs. immutable
 
In article <997a06e0.0404150627.5fb95d9b@posting.google.com >,
pugnatio2@yahoo.com 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).

John Roth 04-15-2004 02:57 PM

Re: Newbie question: Returning mutable vs. immutable
 

<pugnatio2@yahoo.com> wrote in message
news:997a06e0.0404150627.5fb95d9b@posting.google.c 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




All times are GMT. The time now is 01:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.