On 01/23/2015 10:28 PM, Chris Angelico wrote:
>
> cmd = {}
> def command(func):
> cmd[func.__name__] = func
> return func
>
> @command
> def foo(*args):
> print("You asked to foo.")
>
> but this is hardly generic. There's no convenient way to give an
> argument to a decorator that says "please assign this here", short of
> using some stupid eval hack... is there?
If the non-generic is what you're concerned about:
# not tested
dispatch_table_a = {}
dispatch_table_b = {}
dispatch_table_c = {}
class dispatch:
def __init__(self, dispatch_table):
self.dispatch = dispatch_table
def __call__(self, func):
self.dispatch[func.__name__] = func
return func
@dispatch(dispatch_table_a)
def foo(...):
pass
--
~Ethan~
signature.asc
Description: OpenPGP digital signature
-- https://mail.python.org/mailman/listinfo/python-list
