def options(x,y):
return {
'add': lambda: x+y,
'sub': lambda: x-y,
'mul': lambda: x*y,
'div': lambda: x/y,
}
def _dict_funcs(operator, x, y):
return options(x,y).get(operator, lambda: None)()
x = _dict_funcs('mul',4,5)
print(x)
'''
=======
Output
=======
20
To learn more via Dan Bader video: https://www.youtube.com/watch?v=gllUwQnYVww
'''
Python Emulation if-else with dictionary
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.