Simulating switch statements using slices in Python

KISS? Bleh

--

We choose all the time, programmers know what I am talking about. Every programming language is different, the way we express those decisions as well. But when you come from one language to another, sometimes you are forced to think differently. This happened to me when I was learning Python and I noted that the switch statements don’t exist.

Recently while reading in Medium, I found a publication called How Can We Best Switch in Python? Also I checked the anwers to this StackOverflow question. Then I began to wonder if a more “natural” switch structure could be made in any way. So let’s see what we can achieve!

DISCLAIMER: I will not be responsible if you use any code in this post

If we look at Javascript switch (and in general on any switch), we will found a syntax like this:

So we want to achieve something like that. The first part is easily done in python:

# with a function
def switch(value):
pass
# with a class
class switch:
def __init__(self, value):
pass
switch('Hello')

But we need to create a block with the cases. How we can do that? Do you remember how to access the list content? and how to slice it?

my_list = [1, 2, 3, 4, 5]my_list[0]# slice
my_list[0:3:2]

Then we can do something like:

def switch(value):
return []
switch('hello')[
0:3:2
]

At this point you may be figuring out what I am trying to do. We know that a subscription selects an item of a sequence:

subscription ::=  primary "[" expression_list "]"

This means you can use my_list[expression_1, expression_2, ...] to put a list of “cases” in our simulated switch:

switch('hello')[
case_1: code_1,
case_2: code_2,
default: code_3,
]

Finally, we need to create a class called Switch and implement the __getitem__() method. This is required to process the “cases” and emulate the switch behaviour:

class Switch:
def __getitem__(self, keys):
# implement switch logic here.
pass
switch = Switch()

Then we can write a switch like:

A example how the switch would be

This is the idea, therefore it does not mean that we should use it, but it is interesting to know. Here is the full file of switch class I’ve written to try emulate a switch:

The Switch class draft

We can go further and write a case class too. So we can use our code this way:

A full example using a Case class

We can add more features, but thats the idea. Now we can discuss about it.

Pros

  • Is cleaner and less verbose that if-elif-else structure (if you don’t consider the class)
  • The code shows their intention by itself

Cons

  • Is slower than if-else and dictionary indexed (I’ve not tested yet)
  • Is not a language feature, so you can introduce new problems and break things if you change the implementation. This can introduce hard to debug problems
  • We are using tricks to achieve it, so newer programmers could not easily understand what is happening, or how to use it.

Conclusion

I think this way is more close to a “real” switch, but you can find it ugly, non-pythonic, a disgrace, etc. I’m sure the code above can be improved to go faster and be cleaner. I intend to show that we can use some python mechanisms to emulate the switch statement in another way, so the code is only a draft.

Anyway I think that is not a good idea to use in a real project because it doesn’t add much, it could even be considered a bad practice in some situations, but it was fun.

Happy coding ;)

--

--