The original example is:
numbers = (i for i in range(5))
assert 5 not in numbers
sorted(numbers)
I like this example. It provides an opportunity to improve the
documentation.
The problems goes away if we write any of the following
numbers = [i for i in range(5)]
numbers = tuple(i for i in range(5))
numbers = tuple(range(5))
numbers = list(range(5))
It also goes away if we write any of
numbers = [0, 1, 2, 3, 4]
numbers = (0, 1, 2, 3, 4)
In fact, the various ways of making the problem go away produce either a
list or a tuple 0, 1, 2, 3, 4.
A simpler way to create the problem is
x = iter(range(5))
4 in x
sorted(x) # returns an empty tuple.
If instead we write
x = range(5)
then the problem goes away.
I hope this helps.
--
Jonathan
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/YULI3HTZNK46IKOL7SW6HWTBTUUP774A/
Code of Conduct: http://python.org/psf/codeofconduct/