I have lots of defaults being loaded from a config file and stored in a struct-style class (no methods, just variables).
I then have two classes, one defining a Molecule
, another defining a specific kind of molecule, here called Ligand
which inherits from Molecule
. I want Ligand
to have access to all methods and variables from Molecule
and all variables from DefaultsMixin
. I'm trying to use a mixin for this but I think I'm misusing super()
. A rough outline of the classes are as follows:
class DefaultsMixin:
def __init__(self):
self.memory = 4
self.threads = 2
class Molecule:
def __init__(self, name):
super().__init__(name)
self.name = name
class Ligand(DefaultsMixin, Molecule):
def __init__(self, name):
super().__init__(name)
self.atoms = ['C', 'H']
Inheritance is right to left, hence the order in Ligand()
.
I want to avoid using composition as I want to simply call the defaults by name e.g.
# What I want to achieve using mixin
mol = Ligand('methane')
mol.threads
>>> 2
# What I want to avoid using composition
# (self.defaults = Defaults() inside Ligand class instead of using mixin)
mol = Ligand('methane')
mol.defaults.threads
>>> 2
How can I correctly use super()
to get this mixin to work?
Thanks for any help.