On 2018-03-02 12:20, Chris Angelico wrote:
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):".

But you could have "while (do_something() as x) is not _sentinel". Not sure how proponents and opponents would react to that. Limiting the SLNB to the beginning of block-level statements seems perverse in a way, but also might cut down on gratuitous overuse mixed into all kinds of weird positions in statements.

--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail."
   --author unknown
_______________________________________________
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