Init_components and multiple inheritance

What I’m trying to do:
I want some of my forms to inherit another class so they have a few common methods and properties. Let’s say that the other class is as follows:

class Interface():
    """Class to be inherited alongside FormTemplates"""
    def __init__(self, attr1, attr2):
        self.attr_1 = attr1
        self.attr_2 = attr2
        ...

So, in a form, let’s say I change the class to look like this:

class Form(Interface, FormTemplate):
    def __init__(self, **properties):
        # Set Form properties and Data Bindings.
        self.init_components(**properties)

What is the best way to initialize the super for Interface before the super for FormTemplate and before init_components? I’m guessing init_components calls the super().__init__() for FormTemplate (I’m actually not really sure about what exactly init_components does…), so I can’t call super() before or after init_components. Can I pass the arguments for Interface’s super to init_components? What should be the best way to handle this?

1 Like

So, self.init_components() is actually an alias for FormTemplate.__init__(self)! So you can do:

class Form(Interface, FormTemplate):
    def __init__(self, **properties):
        Interface.__init__(self)
        # Set Form properties and Data Bindings.
        self.init_components(**properties)
5 Likes

That’s exactly what I have been doing! Good to know!

Ahhh! This is very useful information. With this, it is now possible to reason about this situation, e.g.,

  1. order of parameters (parent classes) to the class definition (the Anvil-defined class goes last)
  2. which custom initialization code should go before the init_components call
  3. what parameters should go into the call
  4. which custom initialization code should go after the init_components call
    and
  5. that one should not add one’s own FormTemplate.__init__(self) call in these cases. At best, it’s redundant. At worst, it could screw things up.

Call me biased, but I prefer reading and reasoning to spending hours or days experimenting and stumbling across a dozen ways something does not work. :wink: So I’m very happy to see this kind of information, whenever it gets out.

3 Likes

Exactly! That’s why I love reading manuals and documentations.

All of those things you listed where questions I had. The last mention I found in the forum about what init_components() does only mentions that “they did more in the past” but I was never sure if it was a super call or a specific class init.

I am wondering now if I can replace the code with just

class Form(Interface, FormTemplate):
    def __init__(self, **properties):
        super().__ini__(attr1, attr2, **properties)