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

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

This is the same as

result = []
for x in sequence:
    if condition:

which could be written

[expression for x in sequence if condition break]

Terry Jan Reedy

Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to