stylistic question -- optional return value
Andrew Koenig
ark at research.att.com
Wed Aug 28 14:05:38 EDT 2002
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.
If I know that x is always a scalar, one possibility is to return
either x or (x, y). However, that strategy rules out the possibility
of making x a tuple in a future version of this function. Moreover,
it makes extracting x more complicated than it needs to be.
Another possibility is to return either (x, None) or (x, y). Now
it is easy to extract x from the compound result. However,
that strategy removes None from the set of permissible values for y.
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?
More information about the Python-list
mailing list