Securing a future for anonymous functions in Python

Ian Bicking ianb at colorstudy.com
Fri Dec 31 10:01:45 CET 2004


John Roth wrote:
>> 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.

Naming the function doesn't move it far away.  It changes the order (you 
have to define it before you use it), and it introduces a name.

>> 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.

Sure, technically.  But I'm thinking of real use cases.  One I'm 
familiar with is things like map and filter.  These are generally better 
handled with list expressions (and MUCH more readable as such, IMHO). 
Another is control structures, ala Ruby or Smalltalk.  IMHO, we have all 
the control structures we need -- while, for, if.  Most "novel" control 
structures in Ruby or Smalltalk are just another take on iterators.  The 
exception being callbacks, and perhaps some other lazy evaluation 
situations (though outside of what I think of as "callbacks", I can't 
think of any lazy evaluation situations off the top of my head).

So that's why I think callbacks are important; callbacks in the style of 
Twisted Deferred, GUI events, etc.

>> 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.

OK, I should go further -- even a two-line expression (i.e., an 
expression that contains meaningful vertical whitespace) is 
aesthetically incompatible with Python source.  Which covers any 
anonymous function that is more powerful than lambda.  I'm not arguing 
that it can be abused, but more that it isn't any good even when it's 
not being abused.

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

Yep, I've done a fair amount of Smalltalk and Scheme programming.  I 
don't expect Python to act like them.  I appreciate the motivation, but 
I don't think their solution is the right one for Python.

-- 
Ian Bicking  /  ianb at colorstudy.com  / http://blog.ianbicking.org



More information about the Python-list mailing list