2

Ok, the title needs some work, but I'm not a native speaker. Anyways, I have a class in python (a language that I rarely use) and I have a constructor that takes an optional instance of the class itself, like so:

class ArmSegment:
    def __init__(self, length: float, angle: float, parent: ArmSegment):
        self.length = length
        self.angle = angle
        self.parent = parent

    def calc_endpoint(self) -> (float, float):
        # redacted for length

    def calc_actual_angle(self) -> float:
        if self.parent is None:
            return self.angle
        else:
            return self.angle + self.parent.calc_actual_angle()

This gives me an error, because ArmSegment hasn't been defined yet. How do I fix this?

Typhaon
  • 828
  • 8
  • 27
  • This may be a lame workaround, but those type hints are optional, so you could just drop the `: ArmSegment` from the declaration. But there are also a few feasible solutions in an older question/answer. – tobias_k Sep 17 '20 at 09:46
  • Ah good point, I'm trying to give everything a type hint though (I have a bit of a pet peeve against weakly typed languages).. but maybe I have to make an exception – Typhaon Sep 17 '20 at 09:49

0 Answers0