[Python-ideas] Statement local functions and classes (aka PEP 3150 is dead, say 'Hi!' to PEP 403)

Ka-Ping Yee python at zesty.ca
Thu Oct 13 19:51:30 CEST 2011


Hi,

I dislike "postdef"; it and the leading colon are both mysterious.
I guess that's because I find both of them visually misleading.

In the case of ":", the colon visually binds to the first token, so

     :x = whatever(@...)

looks like an assignment to ":x" and my brain immediately goes
"What's :x?"  I have to really work to force myself to make the
tiny little ":" bind looser than everything else on the line.

In the case of "postdef", the Python syntactic tradition is that
a keyword at the beginning always means "this is a statement" and
always describes the type of statement.  "postdef" breaks both of
these rules.  This:

     postdef return whatever(@...)

is NOT a postdef statement, it's a return statement.  And this:

     postdef button.add_handler(@...)

is not even a statement!  It's an expression.

====

Finally, there's a grammar problem that hasn't been addressed
yet.  What about multi-line statements?  Can you write:

     :if do_something(sorted(items, key=@)):
         def sort_key(item): # ??? where to indent this?
             .... # sort_key body
         ... # if body

I sure hope not!

====

All of the above is making me really like the syntactic variations
that put the ":" at the end of a line.  This is visually natural to me:
a colon introduces a block, and that's a good approximation of what
is happening here.  It also clearly demands one level of indentation,
which seems like a good idea.

And it has this nice property that it signals when you are allowed to
use it: on any line that doesn't *already* end with a colon.  If we
made that the rule, then post-definitions would be allowed in these
kinds of statements, which seems reasonable:

     assert
     assignment
     del
     exec
     expression
     global
     nonlocal
     print
     raise
     return
     yield


--Ping



More information about the Python-ideas mailing list