python - What is more 'pythonic' for 'not' -


i have seen both ways, way more pythonic?

a = [1, 2, 3]  # version 1 if not 4 in a:     print 'is not more pythonic?'  # version 2 if 4 not in a:     print 'this haz more engrish' 

which way considered better python?

the second option more pythonic 2 reasons:

  • it one operator, translating 1 bytecode operand. other line not (4 in a); 2 operators.

    as happens, python optimizes latter case , translates not (x in y) x not in y anyway, implementation detail of cpython compiler.

  • it close how you'd use same logic in english language.

Comments