Make 'def' and 'class' usable within expressions

Shane Hathaway shane at zope.com
Fri Mar 26 15:54:33 CET 2004


Shane Hathaway wrote:
> On Fri, 26 Mar 2004, Ronald Oussoren wrote:
> 
> 
>>On 25-mrt-04, at 22:11, Shane Hathaway wrote:
>>
>>>I just thought of a better way to write this.
>>>
>>>    myHandlers = {}
>>>    myHandlers["click"] = def(x,y):
>>>        print "click at x,y"
>>>    myHandlers["enter"] = def():
>>>        print "mouse entered"
>>>    myHandlers["exit"] = def():
>>>        print "mouse exited"
>>>
>>>I think that's pretty nice.
>>
>>And how would you write in-line callbacks for functions that take 
>>multiple callbacks (such as an on-success and on-error handler)?
> 
> 
> I would write a class.
> 
> obj.add_listener(class):
>     def on_success(self):
>         print 'yay!'
>     def on_error(self):
>         print 'ohh.'

Oops, that's not quite correct.  It should pass an instance rather than 
a class.

obj.add_listener((class)()):
     def on_success(self):
         print 'yay!'
     def on_error(self):
         print 'ohh.'

That's not pretty. :-(

Shane




More information about the Python-list mailing list