1

So I have looked at multiple examples of inheritance and constructors on here and I am a little unsure of myself.

So as I understand it you can call a class constructor within a constructor.

class A(Z):
    def __init__(self):
        super().__init__(self)

Right so... would this be a legitimate class call or should I refactor this?

class SubsManagement(Subs, Database_Tables):

    def __init__(self, db_file):
        Database_Tables.__init__(db_file)
        self.conn, self.cur = Database_Tables.connect_database()

        Subs.__init__(self.cur)

What would be my other options for this? This is a personal project btw.

EDIT: Alright, I looked at using a wrapper as per suggestion, but would that be basically making a chain of ancestor classes? If was a little vague as to my personal situation as the example in the non-cooperative classes does not build a constructor with arguments.

  • Possible duplicate of [Calling parent class \_\_init\_\_ with multiple inheritance, what's the right way?](https://stackoverflow.com/questions/9575409/calling-parent-class-init-with-multiple-inheritance-whats-the-right-way) – Hongyu Wang Jul 26 '18 at 13:55
  • I'm not really sure what the question is. Is it "What's the correct way to write my constructor", or is it "Is there a better solution than multiple inheritance", or maybe something else? – Aran-Fey Jul 26 '18 at 13:57
  • You should read [Python's super() considered super!](https://rhettinger.wordpress.com/2011/05/26/super-considered-super/), to see how using `super` beats explicit references to the parent class. – Patrick Haugh Jul 26 '18 at 13:57
  • To be fair I did ask several questions in the post, Aran-Fey. Would super allow for multiple calls? – Chelsea Piccirilli Jul 26 '18 at 14:13
  • Multiple calls of what? Parent constructors? Yes, but it's often uglier than explicitly calling each constructor individually. It depends on the classes you're inheriting from. You may find [this answer](https://stackoverflow.com/a/50465583/1222951) I wrote useful. – Aran-Fey Jul 26 '18 at 15:03
  • Aran-Fey, that did clear it up. That is simply ingenious and I forgot about automated constructors. Thanks again for this. – Chelsea Piccirilli Jul 26 '18 at 15:26

0 Answers0