0

I have a class(call it ClassC) where one of the attributes is another class(call it ClassB). ClassB actually needs data from ClassC to initialize itself, so I pass ClassC to ClassB using the following syntax:

class ClassC(object):
  def __init__(self):
    self.val = 8
    self.derived_class = ClassB(self)

I'm seeing an error that complains about init() not taking any parameters, but I'm just not seeing where the issue could be arising from. Am I not using super() in the correct way so that ClassB initializes its base class,ClassA, as well?

Traceback (most recent call last): File "python", line 18, in File "python", line 15, in init File "python", line 9, in init TypeError: object.init() takes no parameters

I have reproduced the code below, or you can run it here.

class ClassA(object):
  def __init__(self, initial_class):
    self.val = None
    self.initial_class = initial_class

class ClassB(ClassA):
  def __init__(self, initial_class):
    super(ClassA, self).__init__(initial_class)
    self.val = 7

class ClassC(object):
  def __init__(self):
    self.val = 8
    self.derived_class = ClassB(self)

c = ClassC()
wandadars
  • 1,113
  • 4
  • 19
  • 37
  • 2
    You're using `super` wrong. You have to change `super(ClassA, self)` to `super(ClassB, self)` (or just `super()` if you're using python 3). `super(ClassA, self)` gives you a proxy to the _parent class_ of `ClassA`, which is `object`. – Aran-Fey Apr 20 '18 at 15:02
  • @Aran-Fey Can you submit your answer? – wandadars Apr 20 '18 at 15:25

0 Answers0