[Python-ideas] PEP 572 version 2: Statement-Local Name Bindings

Chris Angelico rosuav at gmail.com
Fri Mar 2 16:07:43 EST 2018


On Sat, Mar 3, 2018 at 7:31 AM, Brendan Barnwell <brenbarn at brenbarn.net> wrote:
> On 2018-03-02 12:20, Chris Angelico wrote:
>>
>> On Sat, Mar 3, 2018 at 7:04 AM, Eric Fahlgren <ericfahlgren at gmail.com>
>> wrote:
>>>
>>>
>>> On Fri, Mar 2, 2018 at 10:27 AM, Jelle Zijlstra
>>> <jelle.zijlstra at 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.
>

If we're going to have something that's syntactically valid in an
expression, I don't see a lot of value in requiring that it be inside
a while or if header. That just leads to confusion and mess down the
road.

ChrisA


More information about the Python-ideas mailing list