fork download
  1. #!/usr/bin/env python3
  2. from functools import wraps
  3. from typing import get_type_hints
  4. from collections import defaultdict
  5.  
  6.  
  7. _funcs_table = defaultdict(dict)
  8.  
  9.  
  10. def overload(func):
  11. arg_type = tuple(get_type_hints(func).values())[0]
  12. _funcs_table[func.__name__][arg_type] = func
  13. @wraps(func)
  14. def wrapper(arg):
  15. return _funcs_table[func.__name__][type(arg)](arg)
  16. return wrapper
  17.  
  18.  
  19. @overload
  20. def f(x: int):
  21. print(f'f(int): {x}')
  22.  
  23.  
  24. @overload
  25. def f(x: str):
  26. print(f'f(str): {x}!')
  27.  
  28.  
  29. @overload
  30. def f2(y: int):
  31. print(f'f2(int): {y}')
  32.  
  33.  
  34. @overload
  35. def f2(y: float):
  36. print(f'f2(float): {y}')
  37.  
  38.  
  39. def main():
  40. f(42)
  41. f('Hello')
  42. f2(43)
  43. f2(43.0003)
  44.  
  45.  
  46. if __name__ == '__main__':
  47. main()
Success #stdin #stdout 0.02s 9716KB
stdin
Standard input is empty
stdout
f(int): 42
f(str): Hello!
f2(int): 43
f2(float): 43.0003