[Python-ideas] a new lambda syntax

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

On 20 Oct 2009, at 20:23 , Stephen J. Turnbull wrote:
> Masklinn writes:
>> On 20 Oct 2009, at 07:37 , Stephen J. Turnbull wrote:
>>> That doesn't look like what "anonymous block" means to me.  It looks
>>> like a lambda.
>> An anonymous block and a lambda are the exact same thing.
> Why use different names for them, then?  At least when discussing
> Python which calls the concept "lambda"?
> </WHINE>
One reason is that the creators of Smalltalk liked renaming stuff. A  
more serious one is that as far as they were concerned, they were  
manipulating code blocks. There are only two places in Smalltalk where  
you can have code (apart from the Workspace, which is equivalent to a  
Python shell): methods (method bodies in Python) and blocks (lambdas).  
Since there are no statements and if/for/while/try/whatever are  
implemented through messages (method calls) and anonymous functions,  
it made sense to use the term blocks to designate, well, blocks. Of  

> No, I'm talking about the exact opposite, I think.  Let's forget Ruby,
> since we're talking about anonymous functions after all, and what I
> "know" about Ruby is all hearsay (and evidently not correctly
> understood at that).  In Lisp, you can do something like this:
> (defvar i)
> (defvar f (lambda () (print i)))
> (do ((i 10 (- i 1)))
>    ((< i 1))
>  (funcall f))
> outputting 10 9 8 7 6 5 4 3 2 1 and returning nil.  Ie, it was my
> understanding of "block" that free variables in the block have what in
> Lisp is called "dynamic scope".
In most languages (that I know of), free variables come from the  
lexical scope not the dynamic one (apart from a pair of lisps, I know  
of no language using a dynamic scope). However as I'm not sure what  
precisely happens in your code `i` could be the same in the lexical  
and the dynamic scope so…

Using Python's syntax:

     def foo():
         i = 5
         return lambda: i

     def bar(fn):
         i = "d"
         print fn()

     fn = foo()

here, if the language is lexically scoped the last line will print "5"  
whereas if it's dynamically scoped it will print "d". In pretty much  
every language I know (and that includes Ruby and Smalltalk) it will —  
as in Python — print "5".

More information about the Python-ideas mailing list