stylistic question -- optional return value

Andrew Koenig ark at research.att.com
Wed Aug 28 20:47:41 CEST 2002


>> From: Andrew Koenig [mailto:ark at research.att.com] 
>> 
>> Suppose I have a function that sometimes returns one value 
>> and sometimes returns two.  What's the cleanest way to define 
>> such an interface?

Bjorn> The only time I've had type errors in my Python programs is when I've
Bjorn> tried to do this with a function. If I *have* to do it, I would probably
Bjorn> chose something along the lines of:

Bjorn>   def fn(args):
Bjorn>       class Tmp: pass
Bjorn>       tmp = Tmp()
Bjorn>       ...
Bjorn>       tmp.x = __
Bjorn>       tmp.y = __
Bjorn>       return tmp

Bjorn> your next question should be "why isn't fn a method of Tmp instead" to
Bjorn> which I would reply "indeed" <wink>

Actually, my next question would be why you didn't use a 2-tuple instead of
defining a class with the same properties.

And my answer to your question is that the function I'm defining is actually
a family of methods in a fairly substantial class hierarchy.






More information about the Python-list mailing list