What is the most pythonic way to check if multiple variables are not None?

后端 未结 5 1573
名媛妹妹
名媛妹妹 2021-01-30 19:48

If I have a construct like this:

def foo():
    a=None
    b=None
    c=None

    #...loop over a config file or command line options...

    if a is not None an         


        
5条回答
  •  一个人的身影
    2021-01-30 20:02

    I know this is an old question, but I wanted to add an answer which I believe is better.

    If all elements which have to be checked are hashable, you could use a set instead of a list or tuple.

    >>> None not in {1, 84, 'String', (6, 'Tuple'), 3}
    

    This is much faster than the methods in the other answers.

    >>> import timeit
    >>> timeit.timeit("all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])")
    1.7880705000000034
    >>> timeit.timeit("None not in [1, 84, 'String', (6, 'Tuple'), 3]")
    0.35424169999998867
    >>> timeit.timeit("None not in (1, 84, 'String', (6, 'Tuple'), 3)")
    0.3454340999999772
    >>> timeit.timeit("None not in {1, 84, 'String', (6, 'Tuple'), 3}")
    0.09577370000002361
    

    Another advantage of this method is that it gives you the correct answer even if someone defines the __eq__ method of a class to always return True. (Of course, if they define the __hash__ method to return hash(None), this method won't work. But nobody should do that, because it would defeat the purpose of defining a hash.)

    class my_int(int):
        def __init__(self, parent):
            super().__init__()
    
        def __eq__(self, other):
            return True
    
        def __hash__(self):
            return hash(super())
    
    print(all(v is not None for v in [1, my_int(6), 2])) # True (correct)
    print(None not in [1, my_int(6), 2])                 # False (wrong)
    print(None not in (1, my_int(6), 2))                 # False (wrong)
    print(None not in {1, my_int(6), 2})                 # True (correct)
    

提交回复
热议问题