Jan-12-2024, 11:22 AM
If the method
If you just want a class with some variables and a nice representation, you could use dataclasses instead:
__str__
is not available, then __repr__
is called instead.If you just want a class with some variables and a nice representation, you could use dataclasses instead:
import math from dataclasses import dataclass @dataclass class PointD: x: int y: int @property def alpha(self): """ alpha for polar coordinates """ return math.degrees(math.atan2(self.y, self.x)) @property def hypot(self): """ Length of hypot """ return math.hypot(self.x, self.y) @property def polar(self): """ Poloar coordinates """ return self.alpha, self.hypot @classmethod def from_ploar(cls, polar): """ Create a point from ploar coordinates polar := (alpha, hypot) """ alpha = math.radians(polar[0]) return cls(math.cos(alpha) * polar[1], math.sin(alpha) * polar[1]) p2 = PointD(7, 9) print(p2) print(f"{p2!s}") print(f"{p2!r}") print(f"{p2.alpha}") print(f"{p2.hypot}") print(f"{p2.polar}") print(PointD.from_ploar(p2.polar))
Output:PointD(x=7, y=9)
PointD(x=7, y=9)
PointD(x=7, y=9)
52.1250163489018
11.40175425099138
(52.1250163489018, 11.40175425099138)
PointD(x=6.999999999999998, y=9.0)
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!