Are multiple return values really harmful? (Re: determining the number of output arguments)

Carlos Ribeiro carribeiro at gmail.com
Wed Nov 17 14:40:22 EST 2004


On Wed, 17 Nov 2004 16:49:03 +1300, Greg Ewing
<greg at cosc.canterbury.ac.nz> wrote:
> Jeremy Bowers wrote:
> > Generally, returning a tuple is either a sign that your return value
> > should be wrapped up in a class, or the function is doing too much.
> 
> While I suspect you may be largely right, I
> find myself wondering why this should be so. We
> don't seem to have any trouble with multiple inputs
> to a function, so why should multiple outputs be
> a bad thing? What is the reason for this asymmetry?

If there is an assymetry, it is in *favor* of returning tuples for
multiple outputs. Using a tuple removes some of the coupling between
the caller and the function. Note that the function signature defines
names for its internal use, and the caller is not required to know the
internal names (unless kw parameter passing is used). Forcing names on
the output introduces *more* coupling, and makes the design less
flexible.

In practical terms, it's important to note that generic utility
functions often do not return a "full class", but just a bunch of
results that may or not be stored into a single class. Requiring that
a class to be defined just to return a bunch of data is overkill.

Finally, I assume that it's considered standard practice in Python to
use tuples to store arbitrary collections of data, pretty much like C
structs. The only difference is that the members are not named (and
thus loosely coupled). This should not be a big deal for *most*
situation. The structure of the return tuple can be documented in the
doc string. For complex structures, returning a dict is good
possibility (although in this case the fact that the names are defined
introduces some degree of coupling). Of course, if the case is complex
enough, declaring a class is the way to go, but then, it's a design
decision.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list