Структуры данных: структуры (Python): различия между версиями
Перейти к навигации
Перейти к поиску
(Новая страница: «== Структуры == * [https://docs.python.org/3/library/dataclasses.html Dataclass (документация)]») |
|||
Строка 1: | Строка 1: | ||
== Структуры == | == Структуры == | ||
* [https://docs.python.org/3/library/dataclasses.html Dataclass (документация)] | * [https://docs.python.org/3/library/dataclasses.html Dataclass (документация)] | ||
+ | |||
+ | <syntaxhighlight lang="python" line> | ||
+ | from dataclasses import dataclass | ||
+ | import math | ||
+ | |||
+ | @dataclass(frozen=False, order=True) | ||
+ | class Point: | ||
+ | x: int | ||
+ | y: int | ||
+ | |||
+ | |||
+ | def getLength(point1: Point, point2: Point) -> float: | ||
+ | return math.sqrt((point1.x - point2.x)**2 + (point1.y - point2.y)**2) | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | p1 = Point(0, 6) | ||
+ | p1.y = 0 | ||
+ | p2 = Point(1, 1) | ||
+ | print(p1) | ||
+ | print(p2) | ||
+ | print(getLength(p1, p2)) | ||
+ | |||
+ | |||
+ | if __name__ == '__main__': | ||
+ | main() | ||
+ | |||
+ | </syntaxhighlight> |
Текущая версия на 07:57, 26 июня 2021
Структуры
1 from dataclasses import dataclass
2 import math
3
4 @dataclass(frozen=False, order=True)
5 class Point:
6 x: int
7 y: int
8
9
10 def getLength(point1: Point, point2: Point) -> float:
11 return math.sqrt((point1.x - point2.x)**2 + (point1.y - point2.y)**2)
12
13
14 def main():
15 p1 = Point(0, 6)
16 p1.y = 0
17 p2 = Point(1, 1)
18 print(p1)
19 print(p2)
20 print(getLength(p1, p2))
21
22
23 if __name__ == '__main__':
24 main()