News for hackers. Stuff that matters.
Re:Five-minute Multimethods in Python by gecko on Wednesday December 07, 2011 @10:47AM (#3324244)
While Smalltalk doesn't have case statements, a) it's trivial to add them, and b) the way you get around it is totally different: rather than use a Dictionary, you instead generally make a pile of tiny classes, each with a fooBar: method, and then simply make sure you've got the right object type and call fooBar: on it indiscriminately--in other words, basically your classic visitor pattern.

That's totally possible in Python, too, but I don't really ever see it done.

Re:Five-minute Multimethods in Python by datashaman on Wednesday December 07, 2011 @11:16AM (#3324388)
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.

Re:Five-minute Multimethods in Python by zeekay on Wednesday December 07, 2011 @12:36PM (#3324807)
I usually just provide a default argument to get:

dispatch.get('two', default_strategy)