any trick to allow anonymous code blocks in python?

David Eppstein eppstein at
Sat Jun 26 06:42:30 CEST 2004

In article <ROydndnd54-eYUHdRVn_iw at>,
 Doug Holton <insert at> wrote:

> > b.OnClick = (lambda : sys.stdout("You clicked me"))
> Yeah, I didn't mention the lambda option.  I was thinking about 
> designing a framework meant for beginners, and I'd rather stay away from 
> lambdas.  I'm surprised no one is even proposing support for anonymous 
> code blocks in Python that support multiple lines, similar to what Ruby, 
> Java, and other languages have.

Presumably the right (but non-Python) syntax for such a code block would 

def b.onClick():
    print "You clicked me"

One reason for not adding this to Python would be the difficulty of 
determining where the expression for the def'd object ends and where the 
argument list for its definition begins.  One could imagine working 
backwards from the colon but I don't think Python's parser can do that, 
and anyway if it's difficult for machines to parse it's also difficult 
for humans to read.

David Eppstein            
Univ. of California, Irvine, School of Information & Computer Science

More information about the Python-list mailing list