[issue28617] Why isn't "in" called a comparison operation?
New submission from wim glenn: Regarding https://docs.python.org/3/library/stdtypes.html#comparisons There is a line at the bottom claiming:
Two more operations with the same syntactic priority, in and not in, are supported only by sequence types (below).
The claim is incorrect because `in` and `not in` are also supported by non-sequence types such as sets, mappings, etc for membership testing. Is there any good reason why we don't include them in the table of comparison operations, and say that there are ten comparison operations in python? They do support comparison chaining in the same way: >>> 'x' in 'xy' in 'xyz' True >>> 0 in {0} in [{0}] True ---------- assignee: docs@python components: Documentation files: patch.diff keywords: patch messages: 280080 nosy: docs@python, wim.glenn priority: normal severity: normal status: open title: Why isn't "in" called a comparison operation? type: enhancement versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7 Added file: http://bugs.python.org/file45359/patch.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
R. David Murray added the comment: It should really say "only by types that support iteration". They are not comparison operations, they are containment predicates. ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
wim glenn added the comment: Well, that wouldn't be true either. Because you can easily implement objects which support membership tests but don't support iteration. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
wim glenn added the comment: I want to add that the grammar explicitly mentions them as comparisons https://docs.python.org/3/reference/grammar.html And they are also listed in the comparisons section of the expressions documentation https://docs.python.org/3/reference/expressions.html#comparisons So the docs seem to be contradicting themselves here. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
Raymond Hettinger added the comment: At a grammar and implementation level, the "in" and "not in" operators are treated like comparisons (same precedence and opcodes), but at a semantic level, I concur with David Murray and don't think of these as being comparisons at all. Accordingly, I prefer the current presentation and agree with David that the note should be revised to say "only by types that support iteration". ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
wim glenn added the comment: Perhaps it's better to call a spade a spade here - if they're implemented as comparisons, then why not document them as comparisons? A colleague has mentioned one point that sets `in` and `not in` apart from the other comparisons in the table: comparisons are generally made between objects of the same type (with the exception of numbers). But membership "comparisons" are made between different types (with the exception of substring checks). Here is an alternate patch which leaves the table alone, but corrects the inaccuracy in the note. ---------- Added file: http://bugs.python.org/file45360/newpatch.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
Fred L. Drake, Jr. added the comment: "in" and "not in" are not comparisons, regardless of implementation mechanics (which could change). They aren't really dependent on iteration, though they often correlate with iteration. I'd rather see them described as "containment tests" or something similar. ---------- nosy: +fdrake _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
Raymond Hettinger added the comment: newpatch.diff looks fine. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
Terry J. Reedy added the comment: "Two more operations with the same syntactic priority, in and not in, are supported ..." could be changed to The containment tests in and not in have the same priority as comparisons and are supported ..." ---------- nosy: +terry.reedy stage: -> commit review versions: -Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
wim glenn added the comment: 1 month later.. is newpatch.diff OK to merge or any further improvements needed? thanks ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28617> _______________________________________
Nick Coghlan <ncoghlan@gmail.com> added the comment: Also see https://bugs.python.org/issue32055 regarding the prospect of bringing the implementation into line with the intuitive semantics, and preventing implicit comparison chaining for containment tests. ---------- nosy: +ncoghlan _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
Change by wim glenn <wim.glenn@gmail.com>: ---------- pull_requests: +8545 stage: commit review -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: I think that "types that are :term:`iterable` or implement the :meth:`__contains__` method" is too low-level for this section. In this section we tell about builtin types. From those the types that support `in` and `not in` are list, tuple, dict, set, frozenset, str, bytes, bytearray, memoryview, and iterator types. We can just enumerate them or use general word. Calling the "sequence types" is not correct, but fortunately all of them are iterable, so that we can say just "iterable types described below". Or enumerate categories of types, as they are named in the below subsections: "iterator types, sequence types, str, binary sequence types, set types and dict", with links to corresponding subsections. ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: New changeset 08bcf647d8a92e4bd47531588b284c6820b7a7ef by Miss Islington (bot) (wim glenn) in branch 'master': bpo-28617 Fixed docs inaccuracies about the types that support membership tests (GH-9086) https://github.com/python/cpython/commit/08bcf647d8a92e4bd47531588b284c6820b... ---------- nosy: +miss-islington _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +8612 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +8613 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: New changeset 3e648f8371e342ccfa663ad77e82a538fcc8c9fb by Miss Islington (bot) in branch '3.7': bpo-28617 Fixed docs inaccuracies about the types that support membership tests (GH-9086) https://github.com/python/cpython/commit/3e648f8371e342ccfa663ad77e82a538fcc... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: New changeset 889f080a4d5cdb1cfb901b953f4b89f3ea806bbe by Miss Islington (bot) in branch '3.6': bpo-28617 Fixed docs inaccuracies about the types that support membership tests (GH-9086) https://github.com/python/cpython/commit/889f080a4d5cdb1cfb901b953f4b89f3ea8... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: Humm, why the bot merges in the master branch? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
Change by wim glenn <wim.glenn@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28617> _______________________________________
participants (8)
-
Fred L. Drake, Jr.
-
miss-islington
-
Nick Coghlan
-
R. David Murray
-
Raymond Hettinger
-
Serhiy Storchaka
-
Terry J. Reedy
-
wim glenn