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

Paul Moore p.f.moore at gmail.com
Wed Apr 11 10:41:04 EDT 2018


On 11 April 2018 at 15:28, Chris Angelico <rosuav at gmail.com> wrote:
> 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?

Yes. It's not relevant to the parsing. It is relevant to the
possibility of errors, as you point out. But once again, it's not the
role of the PEP to prevent people writing bad code.

Anyway, this is mostly nitpicking. I'm not trying to argue that this
is good code, or robust code, or even code that I'd permit within a
mile of one of my programs. All I'm trying to say is that *if* you
want to state that the parentheses are mandatory in chained assignment
expressions, then I think you need to justify it (and my suspicion is
that you don't have a good justification other than "it prevents bad
code" - which is already covered by the part of the PEP that points
out that it's not the job of this PEP to prevent people writing bad
code ;-)).

Personally, I dislike the tendency with recent syntax proposals to
mandate parentheses "to remove ambiguity". In my experience, all that
happens is that I end up never knowing whether parentheses are
required or not - and as a result end up with too many parentheses,
making my code look ugly and encouraging cargo cult style "better add
parens just in case" behaviour (as opposed to the reasonable rule "add
parens for readability").

Paul


More information about the Python-ideas mailing list