123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- from typing import Callable
- # def calculate(x:float, y:float, operation:str='a') -> None:
- # def add():
- # print(x + y)
- # def sub():
- # print(x - y)
- # def div():
- # if y == 0:
- # print("На ноль делить нельзя!")
- # else:
- # print(x/y)
- # def mul():
- # print(x*y)
- # match operation:
- # case "a":
- # add()
- # case "s":
- # sub()
- # case "d":
- # div()
- # case "m":
- # mul()
- # case _:
- # print("Ошибка. Данной операции не существует")
- def get_math_func(operation: str) -> Callable[[int, int], int]:
- def add(a: int, b: int) -> int:
- return a + b
- def subtract(a: int, b: int) -> int:
- return a - b
- if operation == "+":
- return add
- elif operation == "-":
- return subtract
-
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # def create_accumulator():
- # counter = 0
- # def inner(x):
- # nonlocal counter
- # counter += x
- # return counter
- # return inner
- # summator_1 = create_accumulator()
- # print(summator_1(1)) # печатает 1
- # print(summator_1(5)) # печатает 6
- # print(summator_1(2)) # печатает 8
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def create_accumulator(st=0):
- counter = st
- def inner(x):
- nonlocal counter
- counter += x
- return counter
- return inner
- # summator_1 = create_accumulator(100)
- # print(summator_1(1)) # печатает 101
- # print(summator_1(5)) # печатает 106
- # print(summator_1(2)) # печатает 108
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def multiply(st):
- def inner(x):
- nonlocal st
- return x*st
- return inner
- # f_2 = multiply(2)
- # print("Умножение 2 на 5 =", f_2(5)) #10
- # print("Умножение 2 на 15 =", f_2(15)) #30
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- from datetime import datetime
- from time import perf_counter
- def timer():
- start = perf_counter()
-
- def inner():
- return perf_counter() - start
-
- return inner
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def add(a, b):
- return a + b
- def counter(func):
- count = 0
- def inner(*args, **kwargs):
- nonlocal count
- count += 1
- print(f"Функция {func.__name__} вызывалась {count} раз")
- return func(*args, **kwargs)
- return inner
- # q = counter(add)
- # q(10, 20)
- # q(10, 20)
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def create_dict():
- count = 0
- my_dict = {}
- def inner(foo):
- nonlocal count
- count += 1
- my_dict.update({count: foo})
- return my_dict
- return inner
-
- f_1 = create_dict()
- print(f_1('hello'))
- print(f_1(100))
- print(f_1([1, 2, 3]))
-
-
-
|