Issue 28617: Why isn't "in" called a comparison operation? (original) (raw)

Created on 2016-11-04 20:36 by wim.glenn, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
patch.diff wim.glenn,2016-11-04 20:35 suggested changes to stdtypes documentation review
newpatch.diff wim.glenn,2016-11-04 22:08 alternate proposal which corrects language but does not classify 'in' as a comparison. review
Pull Requests
URL Status Linked Edit
PR 9086 merged wim.glenn,2018-09-07 03:33
PR 9171 merged miss-islington,2018-09-11 17:45
PR 9172 merged miss-islington,2018-09-11 17:45
Messages (16)
msg280080 - (view) Author: wim glenn (wim.glenn) * Date: 2016-11-04 20:35
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
msg280083 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-11-04 20:56
It should really say "only by types that support iteration". They are not comparison operations, they are containment predicates.
msg280085 - (view) Author: wim glenn (wim.glenn) * Date: 2016-11-04 21:00
Well, that wouldn't be true either. Because you can easily implement objects which support membership tests but don't support iteration.
msg280086 - (view) Author: wim glenn (wim.glenn) * Date: 2016-11-04 21:02
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.
msg280090 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-11-04 21:11
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".
msg280093 - (view) Author: wim glenn (wim.glenn) * Date: 2016-11-04 22:08
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.
msg280094 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2016-11-04 22:17
"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.
msg280128 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-11-06 03:17
newpatch.diff looks fine.
msg280625 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-11-12 00:45
"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 ..."
msg282472 - (view) Author: wim glenn (wim.glenn) * Date: 2016-12-05 22:15
1 month later.. is newpatch.diff OK to merge or any further improvements needed? thanks
msg306412 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-11-17 07:45
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.
msg325015 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-11 15:04
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.
msg325037 - (view) Author: miss-islington (miss-islington) Date: 2018-09-11 17:44
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/08bcf647d8a92e4bd47531588b284c6820b7a7ef
msg325042 - (view) Author: miss-islington (miss-islington) Date: 2018-09-11 18:13
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/3e648f8371e342ccfa663ad77e82a538fcc8c9fb
msg325044 - (view) Author: miss-islington (miss-islington) Date: 2018-09-11 18:18
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/889f080a4d5cdb1cfb901b953f4b89f3ea806bbe
msg325047 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-11 18:38
Humm, why the bot merges in the master branch?
History
Date User Action Args
2022-04-11 14:58:39 admin set github: 72803
2020-01-09 00:56:03 wim.glenn set status: open -> closedresolution: fixedstage: patch review -> resolved
2018-09-11 18:38:28 serhiy.storchaka set messages: +
2018-09-11 18🔞19 miss-islington set messages: +
2018-09-11 18:13:38 miss-islington set messages: +
2018-09-11 17:45:51 miss-islington set pull_requests: + <pull%5Frequest8613>
2018-09-11 17:45:41 miss-islington set pull_requests: + <pull%5Frequest8612>
2018-09-11 17:44:56 miss-islington set nosy: + miss-islingtonmessages: +
2018-09-11 15:04:47 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2018-09-07 03:33:00 wim.glenn set stage: commit review -> patch reviewpull_requests: + <pull%5Frequest8545>
2017-11-17 07:45:07 ncoghlan set nosy: + ncoghlanmessages: +
2016-12-05 22:15:42 wim.glenn set messages: +
2016-11-12 00:45:55 terry.reedy set versions: - Python 3.3, Python 3.4nosy: + terry.reedymessages: + stage: commit review
2016-11-06 03:17:01 rhettinger set messages: +
2016-11-04 22:17:24 fdrake set nosy: + fdrakemessages: +
2016-11-04 22:08:35 wim.glenn set files: + newpatch.diffmessages: +
2016-11-04 21:11:43 rhettinger set nosy: + rhettingermessages: +
2016-11-04 21:02:00 wim.glenn set messages: +
2016-11-04 21:00:21 wim.glenn set messages: +
2016-11-04 20:56:44 r.david.murray set nosy: + r.david.murraymessages: +
2016-11-04 20:36:00 wim.glenn create