On Sat, Mar 3, 2018 at 7:04 AM, Eric Fahlgren <ericfahlg...@gmail.com> wrote:
>
> On Fri, Mar 2, 2018 at 10:27 AM, Jelle Zijlstra <jelle.zijls...@gmail.com>
> wrote:
>>
>>
>> I wonder if we could have a more limited change to the language that would
>> allow only the as/while use cases. Specifically, that means we could do:
>>
>> while do_something() as x:
>>     print(x)
>
>
> The "while" case is the only part of the PEP that has any traction with me.
> It doesn't add any keywords, scope can be identical to "with" and it cleans
> up a code pattern that is very common.

How often do you have a loop like this where you actually want to
capture the exact condition? I can think of two: regular expressions
(match object or None), and socket read (returns empty string on EOF).
This simplified form is ONLY of value in that sort of situation; as
soon as you want to add a condition around it, this stops working (you
can't say "while do_something() is not _sentinel as x:" because all
you'll get is True). And if you are looking for one specific return
value as your termination signal, you can write "for x in
iter(do_something, None):".

> Every one of these comprehension examples has me scratching my head,
> thinking back a couple hundred (thousand? :) ) posts to Barry's quip,
> "Sometimes a for loop is just better" (or something along those lines).

True, but there's a code smell to an unrolled 'for' loop that could
have been a comprehension had it not been for one trivial point of
pedantry. So there are advantages and disadvantages to each.

ChrisA
_______________________________________________
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