any trick to allow anonymous code blocks in python?

Chris S. chrisks at
Sat Jun 26 06:42:09 CEST 2004

Doug Holton wrote:

> Is there any metaclass trick or something similar to allow anonymous 
> code blocks?
> I'd like to be able to let users do something like this fictitious example:
> b = Button()
> b.OnClick =:
>     print "you clicked me"
> But that would require adding a special "=:" operator to bind a code 
> block to a function.

Why can't b.OnClick simply be a handle for a function encapsulating that 

> Is there any PEP for something like that?  I see 310 might allow:
> b=Button()
> with b.OnClick:
>     print "you clicked me"
> I know I can already do it like these examples:
> def OnClick(self,event):
>     print "you clicked me"
> b.OnClick = OnClick
> or
> b = Button(OnClick=OnClick)
> or subclassing Button.

Then what's the problem? wxPython allows you to Bind() functions to 
events. Why obfuscate the code with unnecessary statements?

More information about the Python-list mailing list