"Bengt Richter" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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
--
http://mail.python.org/mailman/listinfo/python-list