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

前端 未结 6 1142
野的像风
野的像风 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 20:53

    I believe that throwing an exception is a better idea for your situation. An alternative will be the simulation method to return a tuple. The first item will be the status and the second one the result:

    result = simulate(open("myfile"))
    if not result[0]:
      print "error parsing stream"
    else:
      ret= result[1]
    

提交回复
热议问题