-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdef_data.py
More file actions
34 lines (26 loc) · 845 Bytes
/
def_data.py
File metadata and controls
34 lines (26 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# зберігання функцій у структурах даних.
from typing import Callable, Dict
# Визначення функцій
def add(a: int, b: int) -> int:
return a + b
def multiply(a: int, b: int) -> int:
return a * b
def power(exponent: int) -> Callable[[int], int]:
def inner(base: int) -> int:
return base ** exponent
return inner
# Використання power для створення функцій square та cube
square = power(2)
cube = power(3)
# Словник операцій
operations: Dict[str, Callable] = {
'add': add,
'multiply': multiply,
'square': square,
'cube': cube
}
# Використання операцій
result_add = operations['add'](10, 20) # 30
result_square = operations['square'](5) # 25
print(result_add)
print(result_square)