[Python-ideas] Membership of infinite iterators

Nick Coghlan ncoghlan at gmail.com
Tue Oct 17 02:42:35 EDT 2017

On 17 October 2017 at 16:32, Nick Coghlan <ncoghlan at gmail.com> wrote:

> So this sounds like a reasonable API UX improvement to me, but you'd need
> to ensure that you don't inadvertently change the external behaviour of
> *successful* containment tests.

I should also note that there's another option here beyond just returning
"False": it would also be reasonable to raise an exception like
"RuntimeError('Attempted negative containment check on infinite iterator')".

That way currently working code would be semantically unchanged, but code
that otherwise results in an infinite loop would turn into an immediate
exception instead.

The rationale for this approach would be "What you are trying to do doesn't
really make sense, so we're going to complain about it, rather than just
giving you an answer".

The rationale against the error is that "If this item is present, advance
past it, otherwise don't do anything" would be an at least arguably
reasonable operation to request.

Whether "x in itr" is a reasonable *spelling* of that operation would then
be a different question that still favoured the "raise an exception"


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171017/69114f6c/attachment.html>

More information about the Python-ideas mailing list