3 ms·
I sometimes use this pattern in combination with a decorator to register functions which I later dispatch to dynamically. Something like: funcs = {} def r
by js2 3y ago
I sometimes use this pattern in combination with a decorator to register functions which I later dispatch to dynamically. Something like:
funcs = {}
def register(key):
def decorator(func):
funcs[key] = func
return func
return decorator
@register("some_data_key")
def do_something_with_some_value(value):
return value
# later....
def parse(data):
return {
key: func(data.get(key)) for key, func in funcs.items()
}