4 ms·
I like to use defaultdict for this. A strategy dictionary, with a default strategy. dispatch = defaultdict(default_stategy) dispatch['one'] = new_strategy pr
by datashaman 15y ago
I like to use defaultdict for this. A strategy dictionary, with a default strategy.
dispatch = defaultdict(default_stategy)
dispatch['one'] = new_strategy
print dispatch['two'] => default_strategy.
Beats case statements and elif chains.
- zeekay 15y agoI usually just provide a default argument to get: dispatch.get('two', default_strategy)