News for hackers. Stuff that matters.
Re:Five-minute Multimethods in Python by mhb on Wednesday December 07, 2011 @09:10AM (#3323802)
Not the main point but if the if-else pattern gets tedious why is there no switch in Python?
Re:Five-minute Multimethods in Python by icebraining on Wednesday December 07, 2011 @09:36AM (#3323907)
Because a switch can usually be replaced by a dictionary?

    { 1: runOne, 2: runTwo, 3:runThree }[n]()
is equivalent to

    switch(n) { 
        case 1: runOne(); break; 
        case 2: runTwo(); break;
        case 3: runThree(); break;
    }
Re:Five-minute Multimethods in Python by adobriyan on Wednesday December 07, 2011 @09:46AM (#3323945)
This forces programmer to invent dummy names. And Python doesn't support nontrivial lambdas.
Re:Five-minute Multimethods in Python by mhb on Wednesday December 07, 2011 @09:47AM (#3323946)
I've seen the argument about why that's more "pythonic", but it's not compelling. Allowing some syntactic sugar would make the meaning a lot more obvious.
Re:Five-minute Multimethods in Python by DanielRibeiro on Wednesday December 07, 2011 @09:46AM (#3323944)
Also, on Smalltalk (which influenced python) there is no switch statement as well. On the other hand, Smalltalk has no if, while, for statements as well (they are all methods).
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)

Re:Five-minute Multimethods in Python by jronkone on Wednesday December 07, 2011 @03:21PM (#3325766)
Consing for control-flow is not normal, but on Python it is. Python - not even once.
Re:Five-minute Multimethods in Python by tikhonj on Wednesday December 07, 2011 @09:18AM (#3323833)
In this case, a switch statement wouldn't help because you're dealing with a bunch of boolean functions rather than a single value. If you were using something like typeof and switched on the type, a switch statement would make more sense. However, even that would be pretty tedious for code like this, I think.

In general though, I have no idea why Python doesn't have a switch or case statement of some sort.

Re:Five-minute Multimethods in Python by candeira on Wednesday December 07, 2011 @09:53AM (#3323968)
Python doesn't need a switch or case statement because you can write a suite of ad-hoc functions, stick them in a dictionary with your values as keys, and dispatch on the values:

    >>> def first():
    ...     print "a"
    ...
    >>> def second():
    ...     print "beta"
    ...
    >>> caseof = {1: first, 2: second}
    >>> caseof[1]()
    a
    >>> caseof[2]()
    beta
    >>>
Re:Five-minute Multimethods in Python by yummyfajitas on Wednesday December 07, 2011 @09:25AM (#3323867)
Now it's a single value:

    case [typeof(X) for x in X] of:
        [int, int]:
            ...
        [int, float]:
            ...
Re:Five-minute Multimethods in Python by icebraining on Wednesday December 07, 2011 @09:40AM (#3323918)

    { (int, int): methodWithTwoInts,
       (int, float): methodsWithIntAndFloat,
       ...
    }[  tuple([typeof(x) for x in X])  ]()
Re:Five-minute Multimethods in Python by adobriyan on Wednesday December 07, 2011 @09:49AM (#3323956)
Actually this is better that switch (or whatever).

Some advanced dispatch code can notice that some tuples are dispatched more often that the rest and can check for them first.

Ditto for dict lookup.

But this would unexpected for switch code.

Re:Five-minute Multimethods in Python by rplnt on Wednesday December 07, 2011 @09:43AM (#3323930)
It's not necessary. If there is long if/else chain in your code, something is probably wrong. In case of python (and other OO, weakly typed languages).

But my opinion is that it wouldn't hurt to have it there.

Re:Five-minute Multimethods in Python by adobriyan on Wednesday December 07, 2011 @09:18AM (#3323830)
COND, man, COND.