Make 'def' and 'class' usable within expressions

John Roth newsgroups at jhrothjr.com
Fri Mar 26 19:43:07 EST 2004


"Ronald Oussoren" <oussoren at cistron.nl> wrote in message
news:mailman.439.1080284510.742.python-list at python.org...
>
> On 25-mrt-04, at 22:11, Shane Hathaway wrote:
>
> > Glenn Andreas wrote:
> >> Also, limiting to "one per expression" prevents you from doing
> >> something like:
> >>    myHandlers = {
> >>       "click" : def(x,y):
> >>                      print "click at x,y"
> >>       "enter" : def():
> >>                      print "mouse entered"
> >>       "exit"  : def():
> >>                      print "mouse exited"
> >>    }
>
>
> >
> > 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)?

Basically the same way, as far as I can tell. The difficulty is that
the expression gets broken up the same way Glen Andreas' suggestion
does above, and you have to be a bit precise about the indentation.


I like it. By far the most common case is a single def in a statement.
It solves a problem that I've got with interactive fiction, where I
want to create a lot of functions on the module level, and then
stick them into instances (not classes.)

The uncommon case is uncommon enough (except in very specialized
environments) that I think we can accept a bit of aparently bizzare
behavior. At least, I can.

John Roth
>
> Ronald
>
>





More information about the Python-list mailing list