I am looking for a way to set multiple dependent attributes in a class. Maybe this example further illustrates what I am trying to do. Currently I am solving this issue with multiple nested
try statements. This does not seem the right way.
r""" Example on sine-wave, where: a * sin(2 * pi * f * t + phi) = a * sin(omega * t + phi) """ import numpy as np class SineWave: def __init__(self, a: float = 1., phi: float = 0., f: float = None, omega: float = None): self.a = a self.f = f self.phi = phi self.omega = omega self.post_init() def post_init(self) -> None: try: self.f = self.omega / (2 * np.pi) except TypeError: self.omega = 2 * np.pi * self.f def __call__(self, t: float) -> float: return self.a * np.sin(self.omega * t + self.phi) if __name__ == '__main__': sin = SineWave(f=1) print(sin(np.pi))
What is the correct way of doing this?
Source: Python Questions