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

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

  • success ( True )
  • failure ( False )
  • read / parse stream ( None )

My question is: if I should not test True or False , how should I see the result. The following shows how I do 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" 

it's really as simple as deleting the == True part or I have to add the tri-bool data type. I do not want the simulate function to throw an exception, because all I want the external program to do with the error is register it and continue.

+80
python
Jan 07 '10 at 13:32
source share
5 answers

Do not be afraid of Exception! Having your program only to enter and continue is as simple 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 simulation method as to what exactly went wrong if you find an error / lack of error to be not informative enough.

+82
Jan 07 '10 at 13:46 on
source share

 if result is None: print "error parsing stream" elif result: print "result pass" else: print "result fail" 

keep it simple and explicit. Of course, you can predefine the dictionary.

 messages = {None: 'error', True: 'pass', False: 'fail'} print messages[result] 

If you plan to change your simulate function to include more return codes, saving this code may be a problem.

simulate can also throw an exception when parsing errors, in which case you will either catch it here or let it spread the level up, and the print bit will be reduced to a single-line if-else statement.

+80
Jan 07 '10 at 13:33
source share

Never never never speak

 if something == True: 

Never. This is crazy because you are redundantly repeating what is redundantly specified as a rule of redundant conditions for the if statement.

Worse still, never, never, never speak

 if something == False: 

You have not . Feel free to use it.

Finally, doing a == None inefficient. Make a is None . None is a special singleton object, it can be only one. Just check if you have this object.

+15
Jan 07 '10 at 15:15
source share

I would like to emphasize that even if there were situations when if expr : not enough, because you need to make sure that expr True not just different from 0 / None / whatever, you should choose from == for the same reason about which said S. Lott to avoid == None .

It really is a bit more effective, and cherry on the cake is more human readable.

Entrance:

 from time import time t0 = time() print ( ( 1 == 1 ) == True ) t1 = time() print ( ( 1 == 1 ) is True ) t2 = time() print '{:e}s\n{:e}s'.format( t1-t0, t2-t1) 

Exit:

 True True 1.201630e-04s 8.797646e-05s 
+3
May 24 '16 at 12:33
source share

I think throwing an exception is the best idea for your situation. An alternative would be a simulation method for returning a tuple. The first element will be the status, and the second is the result:

 result = simulate(open("myfile")) if not result[0]: print "error parsing stream" else: ret= result[1] 
+2
Jan 07 '10 at 13:39
source share



All Articles