[Python-ideas] Proposal for function expressions

Terry Reedy tjreedy at udel.edu
Mon Jul 13 02:12:00 CEST 2009


Chris Perkins wrote:
> I have a proposal for a language feature - a limited form of function-
> definition expression, similar (superficially) to Ruby's blocks.
> 
> The big problem with making "def" an expression is the indentation of
> the function body. You can't just embed an indented block of code into
> the middle of an expression. The solution I propose (inspired by Ruby)
> is to allow a block of code to be appended to the end of an expression
> in certain, limited circumstances. The block is simply syntactic sugar
> for a local anonymous function definition which is passed as an
> argument to the function call that it follows.
> 
> First, a simple example to give the broad strokes:
> foo() do:
>     BODY
> 
> is equivalent to:
> 
> def ANON():
>     BODY
> foo(ANON)

Add del anon and the equivalence is almost exact.
Call all one-off functions '_' and there is no need to ever delete the 
name and you have

def _(): body
foo(_)

which is nearly identical to your proposed foo(&).

In any case, ideas similar to this have been proposed and discussed ad 
nausem and rejected. Guide decided for Python 3 to neiher delete 
function expressions, which he seriously considered, nor to expand them. 
Feel free to peruse the archives and PEPs.

Terry Jan Reedy




More information about the Python-ideas mailing list