On 17 October 2017 at 17:44, Steven D'Aprano <st...@pearwood.info> wrote:
> On Tue, Oct 17, 2017 at 04:42:35PM +1000, Nick Coghlan wrote: > > > 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')". > > I don't think that works, even if we limit discussion to just > itertools.count() rather than arbitrary iterators. Obviously we > cannot wait until the entire infinite iterator is checked (that > might take longer than is acceptible...) but if you only check a > *finite* number before giving up, you lead to false-negatives: > > # say we only check 100 values before raising > 0 in itertools.count(1) # correctly raises > 101 in itertools.count(1) # wrongly raises > Nobody suggested that, as it's obviously wrong. This discussion is solely about infinite iterators that have closed form containment tests, either because they're computed (itertools.count()), or because they're based on an underlying finite sequence of values (cycle(), repeat()). > If we do a computed membership test, then why raise at all? We quickly > know whether or not the value is in the sequence, so there's no error to > report. > Because we should probably always be raising for these particular containment checks, and it's safe to start doing so in the negative case, since that's currently a guaranteed infinite loop. And unlike a "while True" loop (which has many real world applications), none of these implicit infinite loops allow for any kind of useful work on each iteration, they just end up in a tight loop deep inside the interpreter internals, doing absolutely nothing. They won't even check for signals or release the GIL, so you'll need to ask the operating system to clobber the entire process to break out of it - Ctrl-C will be ignored. I'd also have no major objection to deprecating containment tests on these iterators entirely, but that doesn't offer the same kind of UX benefit that replacing an infinite loop with an immediate exception does, so I think the two questions should be considered separately. Cheers, Nick. -- Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/