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: Docs should say that `x is z or x == z` is used for `x in y` in containers that do not implement `__contains__`
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: cheryl.sabella, docs@python, miss-islington, r.david.murray, rhettinger, ztane
Priority: normal Keywords:

Created on 2017-07-19 11:55 by ztane, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2761 merged ztane, 2017-07-19 11:57
PR 13684 merged miss-islington, 2019-05-30 20:23
Messages (4)
msg298671 - (view) Author: Antti Haapala (ztane) * Date: 2017-07-19 11:55
The doc reference/expressions.srt says that

> For user-defined classes which do not define __contains__() but do 
> define __iter__(), x in y is True if some value z with x == z is 
> produced while iterating over y. If an exception is raised during the 
> iteration, it is as if in raised that exception.

and

> Lastly, the old-style iteration protocol is tried: if a class defines 
> __getitem__(), x in y is True if and only if there is a non-negative 
> integer index i such that x == y[i], and all lower integer indices do 
> not raise IndexError exception. (If any other exception is raised, it 
> is as if in raised that exception).

The documentation doesn't match the implementation, which clearly does `x is y or x == y` to check if `x` is the element `y` from a container. Both the `__iter__` and the index-iteration method test the elements using `is` first. While the document says that `x is x` means that `x == x` should be true, it is not true for example in the case of `nan`:
msg298675 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-07-19 14:07
I think you change is appropriate given that the "equivalent to" for the built in iterators contains the 'is' expression.

However, I also think we should drop that second whole paragraph, and in the previous paragraph say "...but are :term:`iterable`...".  Because conceptually what we do is iterate whatever we're given if iter doesn't return a TypeError, and it is iter that handles the fallback to the older __getitem__ protocol.  I don't see why we should re-explain the iteration protocol in the docs for 'in'.  (I haven't looked at how this is actually implemented, but the implementation ought to be equivalent to that or we will eventually run into bugs.)  I don't think this would result in any loss of precision or understandability, and in fact I think it would make it easier to understand.

See also issue 27605, which also wants to edit this section slightly.
msg344000 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-05-30 20:19
New changeset 2f5b9dcc0a89cbde1499c76df81c36bfd5ef9aa8 by Cheryl Sabella (Antti Haapala) in branch 'master':
bpo-30969: Fix docs about the comparison in absence of __contains__ (GH-2761)
https://github.com/python/cpython/commit/2f5b9dcc0a89cbde1499c76df81c36bfd5ef9aa8
msg344001 - (view) Author: miss-islington (miss-islington) Date: 2019-05-30 20:41
New changeset e8661c1dabc206bf7fe6e302e38fa426aa734dd0 by Miss Islington (bot) in branch '3.7':
bpo-30969: Fix docs about the comparison in absence of __contains__ (GH-2761)
https://github.com/python/cpython/commit/e8661c1dabc206bf7fe6e302e38fa426aa734dd0
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75152
2019-05-30 20:41:25miss-islingtonsetnosy: + miss-islington
messages: + msg344001
2019-05-30 20:23:36miss-islingtonsetpull_requests: + pull_request13572
2019-05-30 20:20:17cheryl.sabellasetstatus: open -> closed
resolution: fixed
stage: resolved
2019-05-30 20:19:45cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg344000
2017-07-19 14:07:31r.david.murraysetnosy: + rhettinger, r.david.murray
messages: + msg298675
2017-07-19 11:57:57ztanesetpull_requests: + pull_request2820
2017-07-19 11:55:53ztanecreate