[Python-ideas] values in vs. values out

Ron Adam rrr at ronadam.com
Fri Jan 21 04:47:15 CET 2011

On 01/17/2011 09:04 AM, Luc Goossens wrote:
> Hi all,
> Thanks to everybody for your feedback!
> So I guess the answer to my question (which - I noticed just now - did not
> end with a question mark), is ... no.
>> If your function is returning a bunch of related values in a tuple, and
>> that tuple keeps changing as you re-design the code, that's a code smell.
> the use cases I have in mind are the functions that return a set of weakly
> related values, or more importantly report on different aspects
> of the calculation;
> an example of the first is a divmod function that returns the div and the
> mod while callers might only be interested in the div;
> examples of the latter are the time it took to calculate the value,
> possible warnings that were encountered, ...

You could use a class instead of a function to get different variations on 
a function.

 >>> class DivMod:
...     def div(self, x, y):
...         return x//y
...     def mod(self, x, y):
...         return x%y
...     def __call__(self, x, y):
...         return x//y, x%y
 >>> dmod = DivMod()
 >>> dmod(100, 7)
(14, 2)
 >>> dmod.div(100, 7)
 >>> dmod.mod(100, 7)

Adding methods, to time and/or get warnings, should be fairly easy.

If you do a bunch of these, you can make a base class and reuse the common 

For timing, logging, and checking returned values of functions, decorators 
can be very useful.


More information about the Python-ideas mailing list