Dec-01-2020, 10:12 AM
Complete different approach, not only about printing/representation. Requires Python 3.7+
The dataclass decorator makes a nice representation from a class and some other convenient stuff. In addition, usually you want to keep points together in a tuple, a sequence or mapping. If you have more than one point, it's better to keep
The dataclass decorator makes a nice representation from a class and some other convenient stuff. In addition, usually you want to keep points together in a tuple, a sequence or mapping. If you have more than one point, it's better to keep
x
, y
and z
together in one object.from dataclasses import dataclass @dataclass class Point: x: int y: int z: int point1 = Point(1, 2, 3) point2 = Point(x=-5, y=10, z=5) print(point1) print(point2)If you want to sort Points by x, y and z,
dataclass
needs the extra argument order
. The Argument frozen
will prevent assigning new objects to the Point object. Then the dataclass
behaves like a tuple
.from dataclasses import dataclass @dataclass(order=True, frozen=True) class Point: x: int y: int z: int point1 = Point(1, 2, 3) point2 = Point(x=-5, y=10, z=5) sorted_points = sorted([point1, point2]) print(sorted_points)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!