stylistic question -- optional return value

Roy Smith roy at
Wed Aug 28 23:12:48 CEST 2002

Hans Nowak <wurmy at> wrote:
> In general, it may be better to use two separate functions, aside... the 
> "sometimes it returns A and something it returns B" is usually not a good 
> idea.

Well, I could imagine wanting to write a function which takes two 
integers and returns their quotient...

# Divide two integers.  If the first is a integer multiple of the second,
# return the quotient, otherwise return the quotient and the remainder.
# Assume for the moment, we're dealing with pre-2.0 division semantics!
def idivide (x, y):
   quotient = x / y
   remainder = x % y
   if remainder == 0:
      return quotient
      return (quotient, remainder)

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 

Now, I'm dying to hear Andrew's explanation of why he actually wants to 
do this :-)

More information about the Python-list mailing list