Question

Which of the following if statements is more Pythonic?

if not a and not b:
    do_something

OR

if not ( a or b ):
    do something

Its not predicate logic so I should use the Python key words because its more readable right?

In the later solution more optimal than the other? (I don't believe so.)

Is there any PEP-8 guides on this?

Byte code of the two approaches(if it matters):

In [43]: def func1():
    if not a and not b:
        return
   ....:     
   ....:     

In [46]: def func2():
    if not(a or b):
        return
   ....:     
   ....:     

In [49]: dis.dis(func1)
  2           0 LOAD_GLOBAL              0 (a)
              3 UNARY_NOT           
              4 JUMP_IF_FALSE           13 (to 20)
              7 POP_TOP             
              8 LOAD_GLOBAL              1 (b)
             11 UNARY_NOT           
             12 JUMP_IF_FALSE            5 (to 20)
             15 POP_TOP             

  3          16 LOAD_CONST               0 (None)
             19 RETURN_VALUE        
        >>   20 POP_TOP             
             21 LOAD_CONST               0 (None)
             24 RETURN_VALUE        

In [50]: dis.dis(func2)
  2           0 LOAD_GLOBAL              0 (a)
              3 JUMP_IF_TRUE             4 (to 10)
              6 POP_TOP             
              7 LOAD_GLOBAL              1 (b)
        >>   10 JUMP_IF_TRUE             5 (to 18)
             13 POP_TOP             

  3          14 LOAD_CONST               0 (None)
             17 RETURN_VALUE        
        >>   18 POP_TOP             
             19 LOAD_CONST               0 (None)
             22 RETURN_VALUE        
Was it helpful?

Solution

I'd say whichever is easier for you to read, depending on what a and b are.

OTHER TIPS

I think both your examples are equally readable, however if I wanted to "push the boat out" on readability I would go with:

not any((a, b))

Since to me this reads much more like English, and hence is the most Pythonic.

Which to use? Whichever is more readable for what you're trying to do.

As to which is more efficient, the first one does do an extra not so it is technically less efficient, but not so you'd notice in a normal situation.

They are equivalent and whether one is faster than the other depends on circumstances (the values of a and b).

So just choose the version which you find most readable and/or understandable.

I personally like the Eiffel approach, put into pythonic form

if a and then b: dosomething

if a and b: dosomething

The first approach differs from the second if a is false. It doesn't evaluate b in the first case, in the second it does.

The or equivalent is "or else"

http://en.wikipedia.org/wiki/Short-circuit_evaluation

and/or are eager.

and then/or else short circuit the evaluation

The nice thing about the syntax is that it reads well, and it doesn't introduce new keywords.

For a piece of code to be Pythonic, it must be both pleasing to the reader in and of itself (readable) and in the context of its surroundings (consistent). Without having the context of this piece of code, a good opinion is hard to give.

But, on the other hand... If I were being Pythonic in my opinion giving I would need to operate consistently with my surroundings, which seem not to take context into consideration (e.g. the OP).

The top one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top