closure.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from datetime import datetime
  2. from time import perf_counter, sleep
  3. def multiply(value):
  4. def inner(x):
  5. return value*x
  6. return inner
  7. def make_repeater(n):
  8. def inner(str):
  9. return(str*n)
  10. return inner
  11. '''
  12. def create_accumulator():
  13. s = 0
  14. def inner(x):
  15. nonlocal s
  16. s += x
  17. return s
  18. return inner
  19. '''
  20. def create_accumulator(start_value=0):
  21. s = start_value
  22. def inner(x):
  23. nonlocal s
  24. s += x
  25. return s
  26. return inner
  27. def countdown(x):
  28. counter = start = x
  29. def inner():
  30. nonlocal counter
  31. if counter <= 0:
  32. print(f"Превышен лимит, вы вызвали более {start} раз")
  33. else:
  34. print(counter)
  35. counter -= 1
  36. return inner
  37. def count_calls():
  38. counter = 0
  39. def inner():
  40. inner.total_calls += 1
  41. return inner.total_calls
  42. setattr(inner, 'total_calls', 0)
  43. return inner
  44. def counter(func):
  45. '''Выводит имя и количество вызовов функции'''
  46. count = 0
  47. def inner(*args, **kwargs):
  48. nonlocal count
  49. count += 1
  50. print(f'Функция {func.__name__} вызывалась {count} раз')
  51. return func(*args, **kwargs)
  52. return inner
  53. def timer():
  54. '''Сколько времени прошло с момента создания замыкания'''
  55. start = perf_counter()
  56. def inner():
  57. return perf_counter() - start
  58. return inner
  59. def create_dict():
  60. key = 1
  61. dct = {}
  62. def inner(value):
  63. nonlocal key
  64. dct[key] = value
  65. key += 1
  66. return dct
  67. return inner
  68. def my_func(a):
  69. def inner(x):
  70. return x - a
  71. return inner
  72. def main():
  73. f_1 = create_dict()
  74. print(f_1('privet'))
  75. print(f_1('poka'))
  76. print(f_1([5, 2, 3]))
  77. f2 = create_dict()
  78. print(f2(5))
  79. print(f2(15))
  80. # x = my_func(10)
  81. # print(x(5))
  82. # f_2 = multiply(2)
  83. # print(f_2(5))
  84. # print(f_2(15))
  85. # repeat_2 = make_repeater(2)
  86. # print(repeat_2('Pizza'))
  87. # print(repeat_2('Pasta'))
  88. # summator_1 = create_accumulator()
  89. # print(summator_1(1)) # печатает 1
  90. # print(summator_1(5)) # печатает 6
  91. # print(summator_1(2)) # печатает 8
  92. # summator_2 = create_accumulator()
  93. # print(summator_2(3)) # печатает 3
  94. # print(summator_2(4)) # печатает 7
  95. # summator_1 = create_accumulator(100)
  96. # print(summator_1(1)) # печатает 101
  97. # print(summator_1(5)) # печатает 106
  98. # print(summator_1(2)) # печатает 108
  99. # counter = count_calls()
  100. # counter()
  101. # counter()
  102. # print(counter.total_calls)
  103. # counter()
  104. # print(counter.total_calls)
  105. if __name__ == '__main__':
  106. main()