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?
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)
Ahhh! This is very useful information. With this, it is now possible to reason about this situation, e.g.,
order of parameters (parent classes) to the class definition (the Anvil-defined class goes last)
which custom initialization code should go before the init_components call
what parameters should go into the call
which custom initialization code should go after the init_components call
and
that one should not add one’s ownFormTemplate.__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. So I’m very happy to see this kind of information, whenever it gets out.
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)