Let's say you have a function f (a, b) and different parameter settings in accordance with the value of some variable x. So you want to execute f with a = 1 and b = 3 if x = 'Monday', and if x = 'Saturday', you want to execute f with a = 5 and b = 9. Otherwise, you will print, what is the value of x is not supported.
I would do
from functools import partial def f(a,b): print("A is %s and B is %s" % (a,b)) def main(x): switcher = { "Monday": partial(f,a=1, b=3), "Saturday": partial(f, a=5, b=9) } if x not in switcher.keys(): print("X value not supported") return switcher[x]()
thus f is not executed when the switch is declared, but on the last line.
source share