What is more 'pythonic' for 'not'

∥☆過路亽.° 提交于 2019-11-26 17:55:14

The second option is more Pythonic for two reasons:

  • It is one operator, translating to one bytecode operand. The other line is really not (4 in a); two operators.

    As it happens, Python optimizes the latter case and translates not (x in y) into x not in y anyway, but that is an implementation detail of the CPython compiler.

  • It is close to how you'd use the same logic in the English language.

Most would agree that 4 not in a is more Pythonic.

Python was designed with the purpose of being easy to understand and intelligible, and 4 not in a sounds more like how you would say it in English - chances are you don't need to know Python to understand what that means!

Note that in terms of bytecode, the two will be identical in CPython (although not in is technically a single operator, not 4 in a is subject to optimization):

>>> import dis
>>> def test1(a, n):
        not n in a


>>> def test2(a, n):
        n not in a


>>> dis.dis(test1)
  2           0 LOAD_FAST                1 (n)
              3 LOAD_FAST                0 (a)
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        
>>> dis.dis(test2)
  2           0 LOAD_FAST                1 (n)
              3 LOAD_FAST                0 (a)
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        
derekerdmann

I believe not in is more widely used.

While the PEP 8 style guide doesn't explicitly discuss the topic, it does consider not in to be its own comparison operator.

Don't forget about The Zen of Python. One of the core tenets of writing Python is that "Readability counts," so go with the choice that is the clearest to read and understand in your code.

Although 4 not in a is to be preferred when the choice is made in isolation, there can be cases when the other choice not 4 in a is to be preferred.

For example, if the spec for the software is written to match not 4 in a then it might be best to leave it matching the spec to aid when checking the software against the spec.

A further example is that one way allows this expression of deteriorating health:

( 4 in well,
  well,
  not well,
  not 4 in well) #!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!