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

Chris Angelico rosuav at gmail.com
Wed Apr 11 10:28:23 EDT 2018


On Thu, Apr 12, 2018 at 12:11 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 11 April 2018 at 14:54, Chris Angelico <rosuav at gmail.com> wrote:
>> Sure, if you're just assigning zero to everything. But you could do
>> that with a statement. What about this:
>>
>> q = {
>>     lambda: x := lambda y: z := a := 0,
>> }
>>
>> Yes, it's an extreme example, but look at all those colons and tell me
>> if you can figure out what each one is doing.
>
> lambda: x := (lambda y: (z := (a := 0)))
>
> As I say, it's the only *possible* parsing. It's ugly, and it
> absolutely should be parenthesised, but there's no need to make the
> parentheses mandatory. (And actually, it didn't take me long to add
> those parentheses, it's not *hard* to parse correctly - for a human).

Did you pick up on the fact that this was actually in a set? With very
small changes, such as misspelling "lambda" at the beginning, this
actually becomes a dict display. How much of the expression do you
need to see before you can be 100% sure of the parsing? Could you do
this if fed tokens one at a time, with permission to look no more than
one token ahead?

ChrisA


More information about the Python-ideas mailing list