func.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from typing import Callable
  2. # def calculate(x:float, y:float, operation:str='a') -> None:
  3. # def add():
  4. # print(x + y)
  5. # def sub():
  6. # print(x - y)
  7. # def div():
  8. # if y == 0:
  9. # print("На ноль делить нельзя!")
  10. # else:
  11. # print(x/y)
  12. # def mul():
  13. # print(x*y)
  14. # match operation:
  15. # case "a":
  16. # add()
  17. # case "s":
  18. # sub()
  19. # case "d":
  20. # div()
  21. # case "m":
  22. # mul()
  23. # case _:
  24. # print("Ошибка. Данной операции не существует")
  25. def get_math_func(operation: str) -> Callable[[int, int], int]:
  26. def add(a: int, b: int) -> int:
  27. return a + b
  28. def subtract(a: int, b: int) -> int:
  29. return a - b
  30. if operation == "+":
  31. return add
  32. elif operation == "-":
  33. return subtract
  34. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. # def create_accumulator():
  36. # counter = 0
  37. # def inner(x):
  38. # nonlocal counter
  39. # counter += x
  40. # return counter
  41. # return inner
  42. # summator_1 = create_accumulator()
  43. # print(summator_1(1)) # печатает 1
  44. # print(summator_1(5)) # печатает 6
  45. # print(summator_1(2)) # печатает 8
  46. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. def create_accumulator(st=0):
  48. counter = st
  49. def inner(x):
  50. nonlocal counter
  51. counter += x
  52. return counter
  53. return inner
  54. # summator_1 = create_accumulator(100)
  55. # print(summator_1(1)) # печатает 101
  56. # print(summator_1(5)) # печатает 106
  57. # print(summator_1(2)) # печатает 108
  58. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. def multiply(st):
  60. def inner(x):
  61. nonlocal st
  62. return x*st
  63. return inner
  64. # f_2 = multiply(2)
  65. # print("Умножение 2 на 5 =", f_2(5)) #10
  66. # print("Умножение 2 на 15 =", f_2(15)) #30
  67. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. from datetime import datetime
  69. from time import perf_counter
  70. def timer():
  71. start = perf_counter()
  72. def inner():
  73. return perf_counter() - start
  74. return inner
  75. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. def add(a, b):
  77. return a + b
  78. def counter(func):
  79. count = 0
  80. def inner(*args, **kwargs):
  81. nonlocal count
  82. count += 1
  83. print(f"Функция {func.__name__} вызывалась {count} раз")
  84. return func(*args, **kwargs)
  85. return inner
  86. # q = counter(add)
  87. # q(10, 20)
  88. # q(10, 20)
  89. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. def create_dict():
  91. count = 0
  92. my_dict = {}
  93. def inner(foo):
  94. nonlocal count
  95. count += 1
  96. my_dict.update({count: foo})
  97. return my_dict
  98. return inner
  99. f_1 = create_dict()
  100. print(f_1('hello'))
  101. print(f_1(100))
  102. print(f_1([1, 2, 3]))