1

I have two classes, one is designed to contain the other

class Thing:
    def __init__(self, parent: Box):
        self.parent = parent
        parent.add_to_box(self)

class Box:
    def __init__(self):
        self.children = []

    def add_to_box(self, item: Thing):
        self.children.append(item)

However I can't reference the class types (parent: Box and item: Thing) within each other, only either one or the other.

How can I reference both class types within each other? And otherwise what is the best way to do this?

S.B
  • 13,077
  • 10
  • 22
  • 49

1 Answers1

2

You can just use strings:

class Box:
    def add_to_box(self, item: "Thing"):
        ...
L3viathan
  • 26,748
  • 2
  • 58
  • 81