3 ms·
You got that containment backwards; switch('a', {'abc': 1}, 'default') #returns 1 ... but you're not wrong, that's godawful. But from the context of the ar
by klyrs 6y ago
You got that containment backwards;
switch('a', {'abc': 1}, 'default') #returns 1
... but you're not wrong, that's godawful. But from the context of the article, they're optimizing for the case where multiple cases evaluate to the same answer. It would make more sense to write
switch('a', {('a','b','c'): 1}, 'default') #returns 1
I'd optimize for performance, personally:
def switch(match, dictionary, default="no match"):
return dictionary.get(match, default)