stylistic question -- optional return value

Paddy McCarthy donald.mccarthy at infineon.com
Thu Aug 29 19:40:07 EDT 2002


Erik Max Francis <max at alcyone.com> wrote in message news:<3D6D3F1D.42BD6B41 at alcyone.com>...
> Andrew Koenig wrote:
> 
> > Paddy> I would suggest something like (True, x, y) and (False, x, y)
> > Paddy> where False means disregard any y value. You could then
> > Paddy> consistently assign the return value of your function to three
> > Paddy> variables and examine the first to see if y contains useful
> > Paddy> data.
> > 
> > Hmmm... I think I like the (x, y) or (x, sentinel) idea better,
> > assuming that (x, y) or (x, None) isn't acceptable.
> 
> I agree.  Adding the additional element that acts as a "key" as to what
> follows seems rather distasteful.  I've done this a few times, mostly
> for quick hacks, but I've never been particularly proud of it.  (In all
> cases it was not part of some important API that external people would
> have to use.

If there IS no sentinel value i.e. you want to allow x and y to take any 
value then you either have to supply a third flag value as above, or maybe 
encode the return value if return is a tuple of two values then x and y 
are those values, otherwise the return value is a tuple whose first 
element is x.
The later way of doing things is less clear to me than the Paddy approach 
above (posted from my home account).



More information about the Python-list mailing list