I sing the praises of lambda, my friend and savior!

Bengt Richter bokr at oz.net
Tue Oct 12 00:41:11 EDT 2004


On Mon, 11 Oct 2004 22:11:11 GMT, bokr at oz.net (Bengt Richter) wrote:
[...]
>On Mon, 11 Oct 2004 12:36:21 -0400, "Clark C. Evans" <cce at clarkevans.com> wrote:
>>
>[Bengt]
Just want to say this isn't my latest idea ;-) See below:
(the first non-white-space after the opening ':<' determines the suite indentation level,
and the matching >: ends the suite (without necessarily being outdented or on a separate line).

>>| Well, if lambda is removed (or not ;-), I hope an anonymous def 
>>| expression is allowed, so we can write
>>| 
>>|    obj.f = def(args):
>>|                body
       obj.f = lambda args:<
                   body
               >:
    or

       obj.f = lambda args:<
                   body >:

>>| or
>>| 
>>|    obj.f = def(args): body

       obj.f = lambda args:< body >:

>>| 
>>| or
>>| 
>>|    obj.f = (
>>|        def(args):
>>|            body
>>|    )
       There wouldn't be much reason to use the extra parens above.
>>
>>Ewww.  How is this better than lambda? Let's keep lambda, no?
>>
>Sure, lambda is fine, but it doesn't allow statements, so something
>like def is needed. Just dropping the def name and allowing the
>def statement to be an expression seemed an easy step, since the
>name is basically the only difference.
I had second thoughts ;-)

>
>As seen above, finding an indentation style that is easy to read is
>a challeng, especially if the anonymous def is a middle argument to
>a functions call, e.g.,
>
>    func(arg1, (def(x): print x), arg3)

much better (IMO) now:

     func(arg1, lambda x:< print x >:, arg3)

>
>much as with lambda, you may need parentheses to show where the expression ends.
>For a more complex def, we get into multiple lines in the arg list, e.g.,
>expanded,
>
>    func(
>        arg1,   # use a new line to get consistent indenting of arg list items
>        (def(x, base=10):
>            """even anonymous defs could have doc strings"""
>            return int(x+'0', base) # weird function
>        ),
>        arg3
>    )


     func(
         arg1,   # use a new line to get consistent indenting of arg list items
         lambda x, base=10:<
             """even anonymous lambdas could have doc strings"""
             return int(x+'0', base) # weird function
         >:,
         arg3
     )

>
>or, more compactly,
>
>    func(arg1,
>         def(x, base=10):
>             """even anonymous defs could have doc strings"""
>              return int(x+'0', base) # weird function
>         ,arg3)

     func(arg1,
          lambda x, base=10:<
              """even anonymous lambdas could have doc strings"""
               return int(x+'0', base) >:, # weird function
          arg3)

One-liner:

     func(arg1, lambda x, base=10:< "weird func doc"; return int(x+'0', base) >:, arg3)


>
>It's a tough style problem. But what would you do with lambda, given that
>we don't have lots of insignificant silly parentheses?

I guess I took a stab at answering my own question ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list