better lambda support in the future?

Terry Reedy tjreedy at udel.edu
Sat Dec 18 03:05:08 EST 2004


"Bengt Richter" <bokr at oz.net> wrote in message 
news:41c3b477.357334890 at news.oz.net...
> Looks like your standard pattern could be updated:
>
> >>> dispatch = {}
> >>>
> >>> def dispvia(name):
> ...     def _(f, name=name):
> ...         dispatch[name] = f
> ...         return f
> ...     return _
> ...
> >>> @dispvia('a')
> ... def handle_a(): pass
> ...
> >>> @dispvia('b')
> ... def handle_b(): pass
> ...
> >>> @dispvia('c')
> ... def handle_c(): pass
> ...
> >>> for t in sorted(dispatch.items()): print '%5s: %r'%t
> ...
>     a: <function handle_a at 0x02EE8E9C>
>     b: <function handle_b at 0x02EE8ED4>
>     c: <function handle_c at 0x02EE8F0C>

To avoid the redundancy of 'a' and '_a', etc, how about (untested):

def dispvia(f):
  dispatch[f.__name__.split('_')[1]] = f
  return f

? (Don't have 2.4 loaded yet)

Terry J. Reedy






More information about the Python-list mailing list