On 8/10/2017 9:42 AM, Steven D'Aprano wrote:
On Wed, Aug 09, 2017 at 01:23:28PM -0700, Chris Barker wrote:

I can't recall the use case(s) at the moment, but I have definitely wanted
a way to break out of a comprehension -- and not always with infinite
iterators.

After all, we have "break" in both for and while loops, so clearly there is
the use case...

In both cases, we use 'break' to mean break. If we want to break comprehensions, I think we should continue to use 'break' to mean break instead of twisting 'while' to mean 'break'.

[expression for x in sequence while condition]

should (I believe) be obvious to anyone who already groks comprehension
syntax. The mapping to a for-loop is admittedly a tad more complex:

result = []
for x in sequence:
     if not condition: break
     result.append(expression)

This is the same as

result = []
for x in sequence:
    if condition:
        result.append(expression)
    else:
        break

which could be written

[expression for x in sequence if condition break]

--
Terry Jan Reedy

_______________________________________________
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