Timeline for Replacements for switch statement in Python?
Current License: CC BY-SA 3.0
18 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 12, 2020 at 20:09 | comment | added | abrac |
Is it possible to have the lambdas with various parameters? I.e. case 'a' could have lambda x: 2*x , case 'b' have lambda x, y: x + y , case 'c' have lambda y: 2*y ...
|
|
Sep 15, 2019 at 7:10 | comment | added | Dave Rove |
You only need to build that dictionary once: result = { <dict of lambdas> } and then call it with result[val](x) . This then becomes a very efficient solution.
|
|
Jun 6, 2018 at 16:34 | comment | added | Tony Suffolk 66 | @ninjagecko - I understand what you are saying - it was just that your comment suggested (well the way I read it) that using 'get' caused extra problem - and I was puzzled. | |
Jun 5, 2018 at 13:47 | comment | added | ninjagecko |
@TonySuffolk66: You misunderstand my phrasing; there's no such thing as a dict re-evaluating its values (which are already evaled in non-lazy python). I just said that answers which construct a dictionary and call .get --to directly return the value-- must have already evaled those values. You would not write {True:f(), False:g()}[x] (O(f+g)) instead of f() if x else g() (O(max(f,g))). The only grace is OP asks for fixed values, but this proves one can't replace a switch statement with a {...} /.get sans lambda if one cares about asymptotics. Of course function call overhead is bad too.
|
|
Jun 4, 2018 at 12:22 | comment | added | Tony Suffolk 66 |
@ninjagecko why do you think using get forces any dictionary to evaluate all of the values - it doesn't. get looks for the key - which is a O(1) result and then if the key doesn't exist will then return the default - it is no worse than : if key in dict: return dict[key] else: return default
|
|
May 22, 2018 at 14:50 | comment | added | dojuba | Not for fixed values, but for costly (time, memory etc) functions this is preferred as the lambdas will not be evaluated during construction of the dictionary. +1 to @blubberdiblub 's comment for clarifying this. | |
Sep 18, 2015 at 13:09 | comment | added | blubberdiblub | @slf Yes, that's exactly what happens. I wish I would've been able to put it as short as you did ;) | |
Sep 18, 2015 at 11:44 | comment | added | slf | @blubberdiblub ah, so it will create all the functions every time, but only evaluate one of them | |
Sep 18, 2015 at 6:11 | comment | added | blubberdiblub |
@slf No, when the control flow reaches that piece of code, it will build 3 functions (through the use of the 3 lambdas) and then build a dictionary with those 3 functions as values, but they remain uncalled (evaluate is slightly ambiguous in that context) at first. Then the dictionary gets indexed via [value] , which will return only one of the 3 functions (assuming value is one of the 3 keys). The function hasn't been called at that point, yet. Then (x) calls the just returned function with x as argument (and the result goes to result ). The other 2 functions won't be called.
|
|
Apr 5, 2015 at 6:43 | comment | added | hamsolo474 - Reinstate Monica | only problem with this method is when you try to store functions you later want to reference properties. | |
Aug 7, 2014 at 23:39 | comment | added | Michael M | What if you need case fall through ? | |
Aug 6, 2014 at 19:04 | comment | added | slf | wouldn't this evaluate all the functions/lambdas every time in all cases, even if it is only returning one of the results? | |
Mar 17, 2014 at 13:48 | comment | added | ninjagecko |
Sadly this is the closest people are going to get. Methods which use .get() (like the current highest answers) will need to eagerly evaluate all possibilities before dispatching, and therefore not only are (not just very but) extremely inefficient and also cannot have side-effects; this answer gets around that issue, but is more verbose. I would just use if/elif/else, and even those take just as long to write as 'case'.
|
|
S Feb 16, 2014 at 5:06 | history | suggested | Michelle Welcks | CC BY-SA 3.0 |
Fixed broken link.
|
Feb 16, 2014 at 5:01 | review | Suggested edits | |||
S Feb 16, 2014 at 5:06 | |||||
Apr 22, 2012 at 21:48 | comment | added | Asher | it maybe isn't a good idea to use lambda in this case because lambda is actually called each time the dictionary is built. | |
Jan 21, 2010 at 17:06 | comment | added | Nick | He's asking for fixed values. Why generate a function to calculate something when it's a lookup? Interesting solution for other problems though. | |
Sep 13, 2008 at 0:41 | history | answered | Mark Biek | CC BY-SA 2.5 |