any trick to allow anonymous code blocks in python?

Chris S. chrisks at NOSPAMudel.edu
Sat Jun 26 07:09:03 CEST 2004


David Eppstein wrote:

> In article <ROydndnd54-eYUHdRVn_iw at comcast.com>,
>  Doug Holton <insert at spam.here> 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 
> be:
> 
> 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.
> 

Plus there's little difference between that and:

def onClick(self):
     print "You clicked me"

where this is a class method. It's redundant notation and contrary to 
Python's "there should be one-- and preferably only one --obvious way to 
do it" philosophy.



More information about the Python-list mailing list