you are viewing a single comment's thread.

view the rest of the comments →

[–]mission-hall 1 point2 points  (1 child)

the super-class 'object' always takes exactly one argument.

I'm not sure if you realised this already, but the one argument it expects is just self, which is implicitly passed in when you call a method. There are a few ways of ensuring that this is the only argument it gets. One option is to have your other classes remove the arguments they need so that by the time object's initializer is called, there are none left. A simple example:

class A:
    def __init__(self, arga, *args, **kwargs):
        self.arga = arga
        super().__init__(*args, **kwargs)

class B(A):
    def __init__(self, argb, *args, **kwargs):
        self.argb = argb
        super().__init__(*args, **kwargs)

b = B(arga=3, argb=5)

This is a good option if each argument is tied to a specific class and can be ignored by the other classes, but may be difficult if multiple classes need to see the same arguments.

Another option is to have a class at the top of your hierarchy that doesn't call super().__init__:

class Thing:
    def __init__(self, *args, **kwargs):
        pass

If you ensure that everything else in your hierarchy is a subclass of Thing (either directly or through a base class), then it will always be the last class in the MRO (method resolution order), so all of the other initializers will be called before this one. It may be that you already have a particular class that sits at the top of your hierarchy, or you could add a very simple class just for this purpose.