stylistic question -- optional return value

Erik Max Francis max at alcyone.com
Wed Aug 28 17:28:49 EDT 2002


Roy Smith wrote:

> This may be a little silly, but in some ways it makes sense.  If I ask
> most people what I get if I divide 6 by 3, the answer will be "2", not
> "2 remainder 0".  There is a certain (possibly warped) beauty in
> having
> this function return a variable number of arguments.  We don't seem to
> have any problem with functions which take a variable number of
> arguments.  Simplicity and orthogonality would say that if you can do
> that, you should be able to return a variable number of arguments as
> well.

I'm not seeing this as a really good example (and of course, we have the
divmod builtin that makes the point more strongly).  If you want to
perform integer division, either you're interested in the full answer
(quotient and remainder) or you're only interested in part of it (either
the quotient or remainder).  If you're interested in both, then it makes
sense to return both values in a uniform format.  This isn't some
awkward situation where, say, remainder is undefined when it divides
evenly; the remainder is zero, so it should appear as a zero in the
2-tuple representing (quotient, remainder).

The times when you're _really_ getting into a seriously heterogeneous
environment (by that I mean the function could return widely different
types, excepting None) where you're returning something that really can
have a variable number of arguments might be a case where you're parsing
a command from a prompt and then returning a representation of the
activity to be performed.  But in that case, the complexity would
probably be high enough that having some sort of Command class which has
an .execute method sounds much more appropriate; the function just
returns an instance of (possibly a subclass of) Command.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list