0

How can I implement th following code structure. class A gets an instance of class B but B must be able to call back to A. currenly A won't run because B is not defined yet. but when i swap them B compains that A is not defined yet. I'fighting the python inheritance here and I'm feeling there must be a good solution to this problem. preferrabley one wich remains the references both fom A to B and B to A.

class A:
    def __init__(self, b: B):
        self.b = b
    
class B:
    def __init__(self):
        self.a = None   # Will be filled later
lsie
  • 23
  • 4
  • You could easily get around the *definition* issue, but how will you instantiate them?! You need an A to instantiate a B which needs an A… it's recursive nonsense. – deceze Aug 14 '20 at 14:09
  • @deceze, I've changed the question after reviewing my scenario. the previous one was indeed impossible. – lsie Aug 14 '20 at 14:25
  • Now it's trivial enough to fix the annotation problem by changing the order of the classes… – deceze Aug 14 '20 at 14:27

0 Answers0