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

前端 未结 6 1089
野的像风
野的像风 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]
    
    0 讨论(0)
  • 2020-11-30 21:02
    if result is None:
        print "error parsing stream"
    elif result:
        print "result pass"
    else:
        print "result fail"
    

    keep it simple and explicit. You can of course pre-define a dictionary.

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

    If you plan on modifying your simulate function to include more return codes, maintaining this code might become a bit of an issue.

    The simulate might also raise an exception on the parsing error, in which case you'd either would catch it here or let it propagate a level up and the printing bit would be reduced to a one-line if-else statement.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 21:06

    There are many good answers. I would like to add one more point. A bug can get into your code if you are working with numerical values, and your answer is happened to be 0.

    a = 0 
    b = 10 
    c = None
    
    ### Common approach that can cause a problem
    
    if not a:
        print(f"Answer is not found. Answer is {str(a)}.") 
    else:
        print(f"Answer is: {str(a)}.")
    
    if not b:
        print(f"Answer is not found. Answer is {str(b)}.") 
    else:
        print(f"Answer is: {str(b)}")
    
    if not c:
        print(f"Answer is not found. Answer is {str(c)}.") 
    else:
        print(f"Answer is: {str(c)}.")
    
    Answer is not found. Answer is 0.   
    Answer is: 10.   
    Answer is not found. Answer is None.
    
    ### Safer approach 
    if a is None:
        print(f"Answer is not found. Answer is {str(a)}.") 
    else:
        print(f"Answer is: {str(a)}.")
    
    if b is None:
        print(f"Answer is not found. Answer is {str(b)}.") 
    else:
        print(f"Answer is: {str(b)}.")
    
    if c is None:
        print(f"Answer is not found. Answer is {str(c)}.") 
    else:
        print(f"Answer is: {str(c)}.")
    
    
    Answer is: 0.
    Answer is: 10.
    Answer is not found. Answer is None.
    
    0 讨论(0)
  • 2020-11-30 21:10

    Never, never, never say

    if something == True:
    

    Never. It's crazy, since you're redundantly repeating what is redundantly specified as the redundant condition rule for an if-statement.

    Worse, still, never, never, never say

    if something == False:
    

    You have not. Feel free to use it.

    Finally, doing a == None is inefficient. Do a is None. None is a special singleton object, there can only be one. Just check to see if you have that object.

    0 讨论(0)
  • 2020-11-30 21:15

    I would like to stress that, even if there are situations where if expr : isn't sufficient because one wants to make sure expr is True and not just different from 0/None/whatever, is is to be prefered from == for the same reason S.Lott mentionned for avoiding == None.

    It is indeed slightly more efficient and, cherry on the cake, more human readable.

    In [1]: %timeit (1 == 1) == True
    38.1 ns ± 0.116 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    In [2]: %timeit (1 == 1) is True
    33.7 ns ± 0.141 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    0 讨论(0)
提交回复
热议问题