Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Why not put <conditions> and <statements> into a dictionary?
- # Dynamic add/remove of conditions keeps code clean, flexible!
- # Think about it!
- # https://plus.google.com/100130971560879475093/posts/aStmtjJthQV
- from functools import partial
- import operator
- def always_true(value):
- return True
- def test_conditions(mapping, value):
- for op, action in mapping.items():
- if op(value):
- action()
- break
- tests = {
- partial(operator.eq, 1): partial(print, 'Equal 1'),
- partial(operator.lt, 1): partial(print, 'Less than 1'),
- always_true: partial(print, 'Else'),
- }
- test_conditions(tests, 1)
- test_conditions(tests, 2)
- test_conditions(tests, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement