3 ms·
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
by tikhonj 15y ago
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.
- 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 >>>