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?
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to