I need a help with a code here, i wanted to implement the switch case pattern in python so like some tutorial said , i can use a dictionary for that but here is my problem:<
Here's something a little different (although somewhat similar to @Tumbleweed's) and arguably more "object-oriented". Instead of explicitly using a dictionary to handle the various cases, you could use a Python class (which contains a dictionary).
This approach provides a fairly natural looking translation of C/C++ switch statements into Python code. Like the latter it defers execution of the code that handles each case and allows a default one to be provided.
The code for each switch class method that corresponds to a case can consist of more than one line of code instead of the single return ones shown here and are all compiled only once. One difference or limitation though, is that the handling in one method won't and can't be made to automatically "fall-though" into the code of the following one (which isn't an issue in the example, but would be nice).
class switch:
def create(self): return "blabla %s" % msg(some_data)
def update(self): return "blabla %s" % msg(other_data)
def delete(self): return "blabla %s" % diff(other_data, some_data)
def _default(self): return "unknown type_"
def __call__(self, type_): return getattr(self, type_, self._default)()
switch = switch() # only needed once
return switch(type_)