0

I have something like this:

from some_module import SomeClass


class BaseClass:
    ...


class AnotherClass(BaseClass, SomeClass):
    def __init__(self, parameter, **kwargs):
        BaseClass.__init__(**kwargs)
        self.parameter

    def some_abstract_method(self, **kwargs):
        # this is an abstract method declared in BaseClass
        # and the arguments **kwargs must be passed here 
        # to respect the interface
        SomeClass.__init__(self.parameter, **kwargs)
        SomeClass.do_something()
        return self

The reason I am doing this is to respect the interface of BaseClass. I am wondering how bad is it to structure the object AnotherClass like this and if there is a better way to achieve what I am trying to do. The only other way I can think of is to do something like this:

class AnotherClass(BaseClass):
    def __init__(self, parameter, **kwargs):
        super().__init__(**kwargs)
        self.parameter

    def some_abstract_method(self, **kwargs):
        self.model = SomeClass(self.parameter, **kwargs)
        self.model.do_something()
        return self.model

But I don't like it because AnotherClass is essentially the same object has SomeClass. Basically, I wonder what the best practice would be here.

2
  • There's not enough here to go on. Please take a look creating a minimal reproducible example. Commented Jul 31, 2019 at 15:13
  • You should really be using super if you are using multiple inheritance. Commented Jul 31, 2019 at 15:26

1 Answer 1

1

I always prefer composition over inheritance if I can. The reasons are manifold, but let's try and numerate a few ones:

  • less state on the surrounding object.
  • not violating the single concern paradigm.
  • not risking inadvertent name clashes.
  • easier to test.
  • looser coupling.

So I would roll with your second solution if you can.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to be sure, using composition is my second way of writing the class?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.