This seems like such a simple task, but I've spend way too much time on this now, without a solution. Here's the setup:
class A(object):
def __init__(self, x=0):
print("A.__init__(x=%d)" % x)
class B(object):
def __init__(self, y=1):
print("B.__init__(y=%d)" % y)
class C(A, B):
def __init__(self, x=2, y=3, z=4):
super().__init__(x=x, y=y)
print("C.__init__(z=%d)" % z)
That's the idea, but of course this results in
TypeError: __init__() got an unexpected keyword argument 'y'
All other attempts have failed in a similar manner, and I could find no resource on the internet with the correct solution. The only solutions included replacing all init params with *args, **kwargs. This does not really suit my needs.
As per request, a real world example:
(This uses a different approach, which has a valid syntax, but produces unwanted results.)
from PyQt5.QtCore import QObject
class Settings(object):
def __init__(self, file):
self.file = file
class SettingsObject(object):
def __init__(self, settings=None):
print("Super Init", settings is None)
self.settings = settings
class MyObject(QObject, SettingsObject):
def __init__(self, param):
print("Entering Init")
QObject.__init__(self)
SettingsObject.__init__(self, settings=Settings(__file__))
self.param = param
print("Leaving Init")
Result:
Entering Init
Super Init True
Super Init False
Leaving Init
I would like the line Super Init True to disappear.
superdoesn't call all superclass methods, just the next one. You currently have no way to getCcorrectly set up, as it inherits from classes with incompatible interfaces.*argsand**kwargscorrectly so they're at least compatible.superis for. I'd recommend reading up on the Liskov substitution principle too.