Comparison with False - something I don't understand

Ben Finney ben+python at benfinney.id.au
Thu Dec 2 16:06:35 EST 2010


Harishankar <v.harishankar at gmail.com> writes:

> On Thu, 02 Dec 2010 22:19:25 +1100, Ben Finney wrote:
>
> > More details of the problem you're trying to solve would help with
> > giving specific advice.
>
> I'm writing functions with multiple points of failure exits. I use
> return False as a way to flag the error condition rather than raising
> exceptions.

That's not much detail. Based only on that, I would say you should be
raising an exception at each “point of failure”, preferably of a type
defined for the purpose, instead of returning False.

> But under certain circumstances, the function can also return empty
> lists

The function returns boolean, list, and None types? That's very much a
case where the function is trying to do too many things with the return
value.

Raise exceptions for exceptional cases, and define the function
interface so that it's doing one clear job only. Often that involves
breaking a complicated function into several collaborating functions
with simpler interfaces.

-- 
 \        “Pinky, are you pondering what I'm pondering?” “Umm, I think |
  `\       so, Brain, but three men in a tub? Ooh, that's unsanitary!” |
_o__)                                           —_Pinky and The Brain_ |
Ben Finney



More information about the Python-list mailing list