stylistic question -- optional return value

Sean 'Shaleh' Perry shalehperry at attbi.com
Wed Aug 28 14:31:53 EDT 2002


On Wednesday 28 August 2002 11:05, 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?
>
> What is the most Pythonic way of solving this problem?

I would try *really* hard not to write this function.  It sounds like 
guaranteed confusion and every call to it will have to parse the output so 
you end up with lots of duplicated code.

If I had to implement it I believe I would use the simpler (x,y) return and 
return (x, None) when there is no y.  Yes this removes None from the set of 
possibilities but that is usually ok.  It also makes the code dealing with 
the function fairly uniform.

But I would seriously reconsider the design first.




More information about the Python-list mailing list