[Python-ideas] PEP 572: Assignment Expressions (post #4)

Chris Angelico rosuav at gmail.com
Thu Apr 12 14:14:24 EDT 2018


On Fri, Apr 13, 2018 at 4:01 AM, Thautwarm Zhao <yaoxiansamma at gmail.com> wrote:
>> > Makes sense. However, couldn't you prevent that by giving with
>> priority over the binding ? As in "(with simple_cm) as value", where
>> > we consider the "as" as binding operator instead of part of the with
>> > statement ? Sure, you could commit suicide by parenthesis, but by
>> > default it'd do exactly what the "with simple_cm as value" currently
>> > does. This does require use of as instead of :=, though. (which was
>> > the point I was trying to make, apologies for the confusion)
>>
>> Does "(with simple_cm) as value" means "with (simple_cm as value)"?
>> If so, it's impossible to let the priority of "with ... as ..." over `as`
>> binding.
>>
>> This is the grammar  of current syntax related to with statement:
>>
>> with_stmt: 'with' with_item (',' with_item)* ':' suite
>> with_item: test ['as' expr]
>>
>> If `as` binding could be used in a general expression, just as
>> `test` is the top of expression, an expression using `as` binding must be
>> in the structure > `test`.
>> In other words, if you write
>>
>> with expr as name:
>>     # do stuff
>>
>> Without doubt it's equivalent to `with (expr as name)`.
>>
>> Or you want to completely change the grammar design of CPython :)
>>
>> thautwarm
>
> Additionally, here is an evidence.
>
> I've just had a look at Chris Angelico's implementation about expression
> assignment, it's  cool, however the problem is still raised.
>
> https://github.com/Rosuav/cpython/blob/0f237048b7665720b5165a40de0ed601c1e82c39/Grammar/Grammar
>
> `as` binding is added at line 111, obviously you cannot separate it from the
> `test` structure(because `test` is the top expr).
>
> testlist_comp: (test|star_expr) ( comp_for | 'as' NAME | (','
> (test|star_expr))* [','] )
>
> It seems that if we're to support expression assignment, `as` binding should
> be declined.
> To be honest I feel upset because I think `expr as name` is really cool and
> pythonic.
>

You're looking at a very early commit there. I suggest looking at the
most recent commits on one of two branches:

https://github.com/Rosuav/cpython/blob/statement-local-variables/Grammar/Grammar
https://github.com/Rosuav/cpython/blob/assignment-expressions/Grammar/Grammar

Those are the two most recent states in my progress towards (a)
statement-local name bindings with "EXPR as NAME", and (b) assignment
expressions with "target := value".

ChrisA


More information about the Python-ideas mailing list