stylistic question -- optional return value
paddy3118 at tiscali.co.uk
Wed Aug 28 22:33:16 CEST 2002
Andrew Koenig wrote:
> Suppose I have a function that sometimes returns one value and sometimes
> returns two. What's the cleanest way to define such an interface?
> For the sake of this discussion, I'll use x to refer to the value that
> is always returned and y to refer to the optional value.
> Yet another possibility is to return (False, x) or (True, x, y).
> Now x is in a common position, so retrieving it is straightforward.
> However, I can obtain the same information content by returning
> (x,) or (x, y). However, I can easily imagine people becoming
> confused by 1-tuples.
> What is the most Pythonic way of solving this problem?
I would suggest something like (True, x, y) and (False, x, y) where
False means disregard any y value. You could then consistently assign
the return value of your function to three variables and examine the
first to see if y contains useful data.
Ta ta! - Pad.
More information about the Python-list