Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import singledispatch, update_wrapper
- def methdispatch(func):
- dispatcher = singledispatch(func)
- def wrapper(*args, **kw):
- return dispatcher.dispatch(args[1].__class__)(*args, **kw)
- wrapper.register = dispatcher.register
- update_wrapper(wrapper, func)
- return wrapper
- class Foo(object):
- @methdispatch
- def Subtraction(self, a, b):
- raise NotImplementedError('Unsupported type')
- @Subtraction.register(bool)
- def _(self, p, q):
- if (p == True) and (q == True) : return False
- if (p == False) and (q == True): return True
- if (p == True) and (q == False): return False
- if (p == False) and (q == True): return False
- @Subtraction.register(list)
- def _(self, a, b):
- if ( a[0] < b[0] <= a[1] ) and (b[0] <= a[1] < b[1]) : return [b[0], a[1]]
- if (a[0] < b[0] <= b[1]) and (b[0] <= b[1] < a[1]): return [b[0], b[1]]
- if (b[0] < a[0] <= b[1]) and (a[0] <= b[1] < a[1]): return [a[0], b[1]]
- if (b[0] <= a[0] < a[1]) and (a[0] < a[1] < b[1]): return [a[0], a[1]]
- if (a[1] < b[0] < b[1]) : return [0, 0]
- if (b[1] < a[0] < a[1]) : return [0, 0]
- q = Foo()
- print(q.Subtraction(True, False))
Add Comment
Please, Sign In to add comment