Hello,
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:
# type can be either create or update or ..
message = { 'create':msg(some_data),
'update':msg(other_data)
# can have more
}
return message(type)
but it's not working for me because some_data or other_data can be None (it raise an error if it's none) and the msg function need to be simple (i don't want to put some condition in it).
the problem here is that the function msg() is executed in each time for filling the dict
unlike the switch case pattern who usually in other programming language don't execute the code in the case
unless it's a match.
is there an other way to do this or i just need to do if elif ...
Update: thank you for all the replies, but actually it's more like this
message = { 'create': "blabla %s" % msg(some_data),
'update': "blabla %s" % msg(other_data)
'delete': "blabla %s" % diff(other_data, some_data)
}
so lambda don't work here and not the same function is called, so it's more like a real switch case that i need , and maybe i have to think on an other pattern .