Securing a future for anonymous functions in Python

Simo Melenius firstname.lastname at iki.fi-spam
Fri Dec 31 17:56:30 EST 2004


Steven Bethard <steven.bethard at gmail.com> writes:

> Simo Melenius wrote:
> > map (def x:
> >          if foo (x):
> >              return baz_1 (x)
> >          elif bar (x):
> >              return baz_2 (x)
> >          else:
> >              global hab
> >              hab.append (x)
> >              return baz_3 (hab),
> >      [1,2,3,4,5,6])
> 
> I think this would probably have to be written as:
...
>               return baz_3(hab)
>       , [1,2,3,4,5,6])
> or:
...
>               return baz_3(hab)
>       ,
>       [1,2,3,4,5,6])
> 
> Note the placement of the comma.  As it is,
>      return baz_3(hab),
> returns the tuple containing the result of calling baz_3(hab):

That one didn't occur to me; creating a one-item tuple with (foo,) has
been odd enough for me: only few times I've seen also the parentheses
omitted.

I did ponder the unambiguousness of the last line, though. One could
suggest a new keyword like "end", but keyword bloat is bad.

(Of course, if we trade the "lambda" keyword for another, new keyword
we're not exactly _adding_ keywords... :))

> It's not horrible to have to put the comma on the next line, but it
> isn't as pretty as your version that doesn't.  Unfortunately, I don't
> think anyone's gonna want to revise the return statement syntax just
> to introduce anonymous functions.

There might not be a return statement: the anonymous function might
conditionally return earlier and have side-effects at the end of the
block (to implicitly return None). So the block-ending would need to
fit after any statement and be strictly unambiguous.


br,
S



More information about the Python-list mailing list