[Python-ideas] Membership of infinite iterators

Nick Coghlan ncoghlan at gmail.com
Tue Oct 17 10:09:26 EDT 2017

On 17 October 2017 at 23:17, Koos Zevenhoven <k7hoven at gmail.com> wrote:

> On Tue, Oct 17, 2017 at 2:46 PM, Serhiy Storchaka <storchaka at gmail.com>
> wrote:
>> 17.10.17 14:10, Nick Coghlan пише:
>>> 1. It's pretty easy to write "for x in y in y" when you really meant to
>>> write "for x in y", and if "y" is an infinite iterator, the "y in y" part
>>> will become an unbreakable infinite loop when executed instead of the
>>> breakable one you intended (especially annoying if it means you have to
>>> discard and restart a REPL session due to it, and that's exactly where that
>>> kind of typo is going to be easiest to make)
>> I think it is better to left this on linters.
> ​Just to note that there is currently nothing that would prevent making
> `for x in y in z`​ a syntax error. There is nothing meaningful that it
> could do, really, because y in z can only return True or False (or raise an
> Exception or loop infinitely).

That was just an example of one of the ways we can accidentally end up
writing "x in y" at the REPL, where "y" is an infinite iterator, since it's
the kind that's specific to "x in y", whereas other forms (like
accidentally using the wrong variable name) also apply to other iterator
consuming APIs (like the ones Serhiy mentioned).


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

More information about the Python-ideas mailing list