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

前端 未结 6 1088
野的像风
野的像风 2020-11-30 20:16

I have a function that can return one of three things:

  • success (True)
  • failure (False)
  • error reading/parsing stream
6条回答
  •  抹茶落季
    2020-11-30 21:05

    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.

提交回复
热议问题