Securing a future for anonymous functions in Python

John Roth newsgroups at
Thu Dec 30 23:24:03 CET 2004

"Ian Bicking" <ianb at> wrote in message 
news:mailman.8620.1104433674.5135.python-list at
> John Roth wrote:
>> The syntax I prefer (and I don't know if it's actually been
>> suggested before) is to use braces, that is { and }.
>> In other words, an anonymous function looks like:
>>    {p1, p2, p3 |
>>      stmt1
>>      stmt2
>>     }
> What's the advantage of something like that over the non-anonymous 
> equivalent:
> def some_func(p1, p2, p3):
>     stmt1
>     stmt2
> I appreciate some of the motivation, but merely avoiding giving something 
> a name doesn't seem like a laudible goal.

Actually, it is a laudable goal. It's always easier to understand
something when it's right in front of your face than if it's
off somewhere else.

This, of course, trades off with two other forces: avoiding
repetition and making the whole thing small enough to

So the niche is small, single use functions. The problem
with lambdas is that they're restricted to one expression,
which is too small.

Languages that are designed with anonymous functions
in mind use them very heavily. Smalltalk is the standard
example, and it's also one of the major (possibly the
only) attraction Ruby has over Python.

Python isn't designed that way, which restricts their
utility to a much smaller niche than otherwise.

> The one motivation I can see for function expressions is callback-oriented 
> programming ...

Well, that's true, but that's a very global statement:
when you pass a function into another routine, it's
essentially a callback.


> Function expressions could get really out of hand, IMHO, and could easily 
> lead to twenty-line "expressions".  That's aesthetically incompatible with 
> Python source, IMHO.

Anything can get  out of hand; there's no way of legislating
good style without restricting the language so much that it
becomes unusable for anything really interesting. Even then
it doesn't work: see COBOL as a really good example of
good intentions gone seriously wrong.

Have you ever programmed in a language that does use
anonymous functions extensively like Smalltalk?

John Roth
> -- 
> Ian Bicking  /  ianb at  / 

More information about the Python-list mailing list