Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is De Morgan's Law Pythonic?

Tags:

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         
like image 393
nialloc Avatar asked Oct 22 '12 13:10

nialloc


People also ask

What is DeMorgan's Law in Python?

De Morgan's laws state that: "not (A and B)" is the same as "(not A) or (not B)" and also, "not (A or B)" is the same as "(not A) and (not B)"

What is the equivalent to De Morgan's Law?

It's called De Morgan's Law, after a famous logician, Augustus de Morgan. De Morgan's Law says that '(P and Q)' is logically equivalent to 'not (not P or not Q)'. If it's logically equivalent, then it should be that '(P and Q)' entails 'not (not P or not Q)' and that 'not (not P or not Q) entails '(P and Q)'.

What is De Morgan's Law in simple words?

De Morgan's First Law states that the complement of the union of two sets is the intersection of their complements. Whereas De Morgan's second law states that the complement of the intersection of two sets is the union of their complements. These two laws are called De Morgan's Law.

What is advantage of using DeMorgan's theorem?

De Morgan's theorem allows large bars in a Boolean Expression to be broken up into smaller bars over individual variables. De Morgan's theorem says that a large bar over several variables can be broken between the variables if the sign between the variables is changed.


2 Answers

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

like image 160
Mike Corcoran Avatar answered Oct 22 '22 13:10

Mike Corcoran


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.

like image 29
Andy Hayden Avatar answered Oct 22 '22 12:10

Andy Hayden