[Python-ideas] Proposal for function expressions

Chris Perkins chrisperkins99 at gmail.com
Tue Jul 14 18:31:25 CEST 2009


On Tue, Jul 14, 2009 at 11:26 AM, MRAB<python at mrabarnett.plus.com> wrote:
>
> Anyway, how would you handle condition expressions, as used in 'if' and
> 'while' statements? /They/ also expect a terminating colon and an
> indented block.

Python 2.7a0 (trunk, Jul 10 2009, 16:43:32) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(a): return a()
...
>>> if f() do: return True:
  File "<stdin>", line 1
    if f() do: return True:
            ^
SyntaxError: invalid syntax
>>> while (f() do: return 1):
  File "<stdin>", line 1
    while (f() do: return 1):
                ^
SyntaxError: invalid syntax
>>>

Yeah, so the error message could be clearer :)  But I think I have
covered all the bases. I think. That's why I'm hoping at least a few
people will apply the patch and find the edge cases that I have
inevitably missed.

To answer your question more specifically: the modification to the
grammar allows a block following a "simple_stmt" only. Here's the
relevant bit:

stmt: simple_stmt | compound_stmt
# additional restrictions enforced by the interpreter - only certain
# types of small_stmt are allowed to have a block attached:
simple_stmt: small_stmt (';' small_stmt)* (([';'] NEWLINE) | block)
block: 'do' [parameters] ':' suite

The cases you are concerned about are "compount_stmt"s, so a block is
not allowed.

Chris Perkins



More information about the Python-ideas mailing list