fork download
  1. class aritydispatch:
  2. def __init__(self, default):
  3. self.__default = default
  4. self.__arities = {}
  5.  
  6. def __call__(self, *args, **kwargs):
  7. f = self.__arities.get(len(args), self.__default)
  8. return f(*args, **kwargs)
  9.  
  10. def register(self, n):
  11. def decorator(f):
  12. self.__arities[n] = f
  13. return decorator
  14.  
  15. @aritydispatch
  16. def f():
  17. assert False
  18.  
  19. @f.register(1)
  20. def _(a):
  21. print(a)
  22.  
  23. @f.register(2)
  24. def _(a, b):
  25. print('{} {}'.format(a, b))
  26.  
  27. f(1, 2)
  28. f(1)
  29. f()
  30.  
Success #stdin #stdout 0.02s 9936KB
stdin
Standard input is empty
stdout
1 2
1