Comparison with False - something I don't understand

Steve Holden steve at holdenweb.com
Thu Dec 2 10:19:35 EST 2010


On 12/2/2010 9:13 AM, Harishankar wrote:
> 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. But under certain circumstances, the function can also return 
> empty lists which equate to false when using the condition like:
> 
> # myfunction () can return a list of tuples, but can also return an empty
> # list under certain conditions since it's query a database and the result
> # can be empty also
> 
> result = myfunction (vars) 
> 
> if not result:
>     # error condition
> 
> Now above I first realized that the function can also return an empty 
> list under some conditions and so changed it to
> 
> if result == False:
>     # error condition
> 
> 
> But now I realize that it's better to use "is"
> 
> if result is False:
>     # error condition
> 
> That is how my problem arose.
> 
Did you think about using exceptions to handle exceptional conditions?
If you are new to Python it may not be the obvious soltuion, but it can
greatly simplify program logic.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17       http://us.pycon.org/
See Python Video!       http://python.mirocommunity.org/
Holden Web LLC                 http://www.holdenweb.com/




More information about the Python-list mailing list