In Python how should I test if a variable is None, True or False

python

Solution

Don't fear the Exception! Having your program just log and continue is as easy as:

try:
    result = simulate(open("myfile"))
except SimulationException as sim_exc:
    print "error parsing stream", sim_exc
else:
    if result:
        print "result pass"
    else:
        print "result fail"

# execution continues from here, regardless of exception or not

And now you can have a much richer type of notification from the simulate method as to what exactly went wrong, in case you find error/no-error not to be informative enough.

Problem

I have a function that can return one of three things: - success (`True`) - failure (`False`) - error reading/parsing stream (`None`) My question is, if I'm not supposed to test against `True` or `False`, how should I see what the result is. Below is how I'm currently doing it: ``` result = simulate(open("myfile")) if result == None: print "error parsing stream" elif result == True: # shouldn't do this print "result pass" else: print "result fail" ``` is it really as simple as removing the `== True` part or should I add a tri-bool data-type. I do not want the `simulate` function to throw an exception as all I want the outer program to do with an error is log it and continue.

Original source

Related problems