[Python-ideas] Proposal for function expressions

Carl Johnson cmjohnson.mailinglist at gmail.com
Wed Jul 15 02:22:34 CEST 2009


It may be possible to work out the grammar so that & is never
ambiguous between "insert block here" and "and these two things
together." I'd need to think about it some more to be sure. One
advantage of using & as a sigil is that it jives with what Rubyists
are used to. That said, I have to insist that what you've proposed as

f() do:
    BLOCK

be written instead as

f(&) do:
    BLOCK

Just magically inserting the block as the last argument to a call is
something I hate about Ruby, and I think their experience has shown
that it's a mistake, since it hurts flexibility. EIBTI, etc.

To those claiming this can all be done with decorators, see another
one of my old threads: "Allow Lambda Decorators"
http://mail.python.org/pipermail/python-ideas/2009-February/002787.html

It is true that instead of using

new_list = sorted(old_list, key=&) do item:
    name = item.split("-")[0]
    return name.lower()

one might do

@list_sorted(oldlist)
def new_list(item):
    name = item.split("-")[0]
    return name.lower()

with list_sorted defined as

def list_sorted(seq):
    def inner(f):
        return sorted(seq, key=f)
    return inner

But the problem with this is that the reader of your code will see the
"@" and think "here comes the definition of a callable." It's only
after a lot of thinking that the reader will realize that new_list is
a list, not a callable. So, in terms of readability, using decorators,
though possible, is probably a mistake.

-- Carl Johnson



More information about the Python-ideas mailing list