[Python-Dev] Examples for PEP 572

Steven D'Aprano steve at pearwood.info
Wed Jul 4 09:31:25 EDT 2018


On Wed, Jul 04, 2018 at 01:03:02AM -0700, Devin Jeanpierre wrote:

> The PEP doesn't talk about it, but FWIW, Go and C++17 if statements
> allow you to put arbitrary simple statements in the suite header, and
> by doing that, solves all the issues you and the PEP mentioned.

Whether they solve "all the issues" discussed in the PEP is doubtful, 
since they're entirely limited to just if statements.

It amuses me that the Go designers -- presumably Rob Pike -- decided on 
a rule "no assignment expressions", and then immediately broke it "well 
okay, assignment expressions in if statements". At least Guido waited 
20+ years to change his mind :-)


> In Python it'd look like this:
> 
> if x = expr(); x < 0:
>   do_stuff()


That's even more doubtful, since the semicolon in Python already has a 
meaning of separating statements on a line. That could be equivalent to:

if x = expr()
x < 0:
     do_stuff()

which would clearly have to be a syntax error. *Two* syntax errors.

Python's parser is restricted to LL(1) by design, so treating semicolons 
in "if" statements as a special case might not even be possible. 
But even if it is possible, not only would this special case 
break the Zen, but it would be UGLY too.


-- 
Steve


More information about the Python-Dev mailing list