[Python-ideas] Enhance definition of functions

Haoyi Li haoyi.sg at gmail.com
Wed Jul 31 08:37:38 CEST 2013


I'd like multiline lambdas too, but the syntax is a thorny problem. F# is
the only language I know (are there others?) that allows you to mix
whitespace-delimited and paren-delimited expressions, e.g. with
whitespace-blocks inside parens:

let f n = n + (
    if n % 2 = 0 then
        printf "lol"
        1
    else
        printf "omg"
        2
)

f 0
// lol
// 1
f 1
// omg
// 3
f 2
// lol
// 3

And it actually works as you'd expect, most of the time, so clearly doing
such a thing is possible.

On the other hand, even with lots of smart people behind F#, edge cases in
the parsing of this sort of thing bites me pretty regularly. Although It'd
be awesome if it could be implemented cleanly and predictably, I definitely
do not want to be the one writing the grammar or parser to support this
kind of syntax!

-Haoyi


On Wed, Jul 31, 2013 at 2:23 PM, Ronald Oussoren <ronaldoussoren at mac.com>wrote:

>
> On 31 Jul, 2013, at 3:41, Terry Reedy <tjreedy at udel.edu> wrote:
>
> > On 7/30/2013 11:59 AM, Ronald Oussoren wrote:
> >
> >> "Never" is a long time. AFAIK the main reason why Python doesn't have
> >> multi-line lambda's is that nobody has proposed a suitable syntax yet
> >> (and not for lack of trying, the archives of this list and python-dev
> >> contain a lot of proposals that were found lacking).
> >
> > There is also the fact that a generic .__name__ attribute of '<lambda>'
> is inferior to a possibly unique and meaningful name. This is not just in
> tracebacks. Consider
> >  [<built-in function sin>, <built-in function cos>]
> > versus
> >  [<function <lambda> at 0x0000000003470B70>, <function <lambda> at
> 0x0000000003470BF8>]
>
> It might be lack of imagination on my part, but I have a lot of nested
> functions named "function" or "callback" that are too complex to be a
> lambda, but too simple or specialized to bother making them proper
> functions. The key function for sort is one of the usecases.
>
> I'd love to have anonymous functions for that, but haven't seen a proposal
> for those yet that would fit the language.
>
> Ronald
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130731/5173b419/attachment.html>


More information about the Python-ideas mailing list