Recently, I saw some discussions online about how there is no good "switch / case" equivalent in Python. I realize that there are several ways to do something similar - some with lambda, some with dictionaries. There have been other StackOverflow discussions about the alternatives. There were even two PEPs (PEP 0275 and PEP 3103) discussing (and rejecting) the integration of switch / case into the language.
I came up with what I think is an elegant way to do switch / case.
It ends up looking like this:
from switch_case import switch, case # note the import stylex = 42
switch(x) # note the switch statement
if case(1): # note the case statementprint(1)
if case(2):print(2)
if case(): # note the case with no argsprint("Some number besides 1 or 2")
So, my questions are: Is this a worthwhile creation? Do you have any suggestions for making it better?
I put the include file on github, along with extensive examples. (I think the entire include file is about 50 executable lines, but I have 1500 lines of examples and documentation.) Did I over-engineer this thing, and waste a bunch of time, or will someone find this worthwhile?
Edit:
Trying to explain why this is different from other approaches:
1) Multiple paths are possible (executing two or more cases), which is harder in the dictionary method.
2) can do checking for comparisons other than "equals" (such as case(less_than(1000)).
3) More readable than the dictionary method, and possibly if/elif method
4) can track how many True cases there were.
5) can limit how many True cases are permitted. (i.e. execute the first 2 True cases of...)
6) allows for a default case.
Here's a more elaborate example:
from switch_case import switch, case, betweenx=12
switch(x, limit=1) # only execute the FIRST True case
if case(between(10,100)): # note the "between" case Functionprint ("%d has two digits."%x)
if case(*range(0,100,2)): # note that this is an if, not an elif!print ("%d is even."%x) # doesn't get executed for 2 digit numbers,# because limit is 1; previous case was True.
if case():print ("Nothing interesting to say about %d"%x)# Running this program produces this output:12 has two digits.
Here's an example attempting to show how switch_case can be more clear and concise than conventional if/else:
# conventional if/elif/else:
if (status_code == 2 or status_code == 4 or (11 <= status_code < 20) or status_code==32):[block of code]
elif status_code == 25 or status_code == 45:[block of code]
if status_code <= 100:[block can get executed in addition to above blocks]# switch_case alternative (assumes import already)
switch(status_code)
if case (2, 4, between(11,20), 32): # significantly shorter![block of code]
elif case(25, 45):[block of code]
if case(le(100)):[block can get executed in addition to above blocks]
The big savings is in long if statements where the same switch is repeated over and over. Not sure how frequent of a use-case that is, but there seems to be certain cases where this makes sense.
The example file on github has even more examples.