Securing a future for anonymous functions in Python

Bengt Richter bokr at oz.net
Fri Dec 31 21:56:33 EST 2004


On 01 Jan 2005 00:56:30 +0200, Simo Melenius <firstname.lastname at iki.fi-spam> wrote:

>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.
ISTM you don't need "end" -- just put the def expression in parens,
and let the closing paren end it, e.g.:

     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])

>
>(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.
Just use parens as necessary or when in doubt ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list