by icebraining · 14y ago · 0 comments
{ 1: runOne, 2: runTwo, 3:runThree }[n]()
is equivalent to switch(n) {
case 1: runOne(); break;
case 2: runTwo(); break;
case 3: runThree(); break;
}That's totally possible in Python, too, but I don't really ever see it done.
dispatch = defaultdict(default_stategy)
dispatch['one'] = new_strategy
print dispatch['two'] => default_strategy.
Beats case statements and elif chains.
dispatch.get('two', default_strategy)