This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PEP 8 should recommend "is not" and "not in"
Type: enhancement Stage:
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, docs@python, flox, icordasc, myint, ncoghlan
Priority: normal Keywords:

Created on 2014-05-20 05:59 by flox, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg218837 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2014-05-20 05:59
Python accepts both syntaxes:

    if not item in some_list and not item is None:
        return item

and

    if item not in some_list and item is not None:
        return item


In the first form, you identify 5 operators: "not", "in", "and", "not" and "in"
In the second form, you find only 3 operators: "not in", "and", "is not"


Of course CPython does internal optimization, and it compiles both expressions to the same bytecode.

However the second form is more readable and less error-prone. It is plain English.

I propose to add such advice to the section "Programming Recommendations" of PEP 8.
msg218858 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-05-20 22:10
Agreed!  I'll update the PEP.  Thanks.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65739
2014-05-20 22:14:34barrysetstatus: open -> closed
resolution: fixed
2014-05-20 22:10:15barrysetassignee: barry
messages: + msg218858
2014-05-20 14:30:29icordascsetnosy: + icordasc
2014-05-20 13:16:44myintsetnosy: + myint
2014-05-20 06:28:01floxsetassignee: docs@python -> (no value)
2014-05-20 05:59:43floxcreate