Comparison with False - something I don't understand

Harishankar v.harishankar at gmail.com
Thu Dec 2 09:13:05 EST 2010


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.

- 
Harishankar (http://harishankar.org http://lawstudentscommunity.com)




More information about the Python-list mailing list