4 ms·
Not the main point but if the if-else pattern gets tedious why is there no switch in Python?
by mhb 15y ago
Not the main point but if the if-else pattern gets tedious why is there no switch in Python?
- adobriyan 15y agoCOND, man, COND.
- tikhonj 15y agoIn 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.
- yummyfajitas 15y agoNow it's a single value: case [typeof(X) for x in X] of: [int, int]: ... [int, float]: ...
- icebraining 15y ago{ (int, int): methodWithTwoInts, (int, float): methodsWithIntAndFloat, ... }[ tuple([typeof(x) for x in X]) ]()
- adobriyan 15y agoActually 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.
- candeira 15y agoPython 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 >>>
- icebraining 15y agoBecause 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; }
- DanielRibeiro 15y agoAlso, 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).
- gecko 15y agoWhile 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.
- datashaman 15y agoI 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)
- adobriyan 15y agoThis forces programmer to invent dummy names. And Python doesn't support nontrivial lambdas.
- rplnt 15y agoIt'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.