[Python-ideas] a new lambda syntax

Masklinn masklinn at masklinn.net
Tue Oct 20 13:34:11 CEST 2009


On 20 Oct 2009, at 13:21 , Steven D'Aprano wrote:
>
> You don't pass compound statements around as input to other functions,
> so you know the environment that they're being called in, and can more
> easily reason about their correctness.
>
But unless the language is dynamically scoped (which is getting pretty  
rare these days, and which Python definitely isn't) the environment in  
which the block is executed is not quite relevant, since the free  
variables are bound based on its lexical scope (the environment in  
which the block is created) and the rest is made of argument (as in  
every other function).

> In my opinion, it simply isn't enough of a burden to define a function
> before you use it, using a "don't care" name, to deserve the amount of
> words written about potential multi-statement lambda syntax. If Python
> had different syntax, then perhaps we'd have anonymous functions like
> Ruby. But we don't, and frankly I don't think it is much of a
> restriction.
>
The biggest (by far) advantages I see to good anonymous functions  
(note: Ruby's aren't, as far as I'm concerned, because due to their  
nature they don't easily scale from 1/call to 2+/call) are in  
flexibility, freedom of experimentation and possibility to keep the  
core language itself small: had Python had "full-blown" anonymous  
functions, it wouldn't have been necessary to add the `with` statement  
to the language. It could just as well have been implemented through a  
protocol or the stdlib, and people would have been free to toy with it  
in their projects long before it was added to the core language.

It also gives the possibility of making OO much less imperative (right  
now if one wants customized code paths, one usually has to rely on  
extracting data from objects, performing imperative operations and  
then feeding the result back to some other object, but objects could  
just as well provide "hooks" thought callables), and of creating  
custom control structures (see `with` above, but it applies to far  
more stuff).

In fact, Smalltalk quite clearly demonstrated that with messages  
(method calls) and blocks (anonymous functions) you basically didn't  
need "hard-coded" control structures anymore. Now Smalltalk probably  
went a bit too far for most people, even 30 years later, but it does  
show how powerful this combination is, and how it allows the  
language's users to evolve the language itself, without having to  
change its core syntax.



More information about the Python-ideas mailing list