[Python-ideas] Membership of infinite iterators
Jason Campbell
j_campbell7 at hotmail.com
Sun Oct 15 21:12:08 EDT 2017
I recently came across a bug where checking negative membership (__contains__ returns False) of an infinite iterator will freeze the program.
It may seem a bit obvious, but you can check membership in range for example without iterating over the entire range.
`int(1e100) in range(int(1e101))` on my machine takes about 1.5us
`int(1e7) in itertools.count()` on my machine takes about 250ms (and gets worse quite quickly).
Any membership test on the infinite iterators that is negative will freeze the program as stated earlier, which is odd to me.
itertools.count can use the same math as range
itertools.cycle could use membership from the underlying iterable
itertools.repeat is even easier, just compare to the repeatable element
Does anyone else think this would be useful?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171016/2a580410/attachment.html>
More information about the Python-ideas
mailing list