0

I have two classes which extend two different base classes. Those classes setup custom environment.

The are three methods common in both the classes: prepare(), setup() and teardown(). Each method overrides base class method. (Also they have super() calls.) Also ExtendedBase extends Base.

First class:

ClassA(Base):
    def __init__(self, args):
        super().__init__(args)

        self._private = OtherClass()

    def prepare(self, args):
        super().prepare(args)
        self._private.prepare()

    def setup(self, args):
        super().setup(args)
        self._private.setup(self._smth, args)

    def teardown(self):
        self._private.teardown()
        super().teardown()

Second class:

ClassB(ExtendedBase):
    def __init__(self, args):
        super().__init__(args)

        self._private = OtherClass()

    def prepare(self, args):
       super().prepare(args)
       self._private.prepare()

    def setup(self, args):
       super().setup(args)
       self._private.setup(self._smth, args)

    def teardown(self):
       self._private.teardown()
       super().teardown()

Is this a way to avoid duplicate methods? I thought about multiple inheritance with the Environment class which will contain duplicated methods, but am puzzled about how to implement this and if it is a good idea at all.

Edited: Unfortunately I couldn't do anything with classes hierarchy. ClassA and ClassB should inherit corresponding classes and override or use the parent's method.

2
  • 1
    Maybe you could invert your class hierarchy and put those three methods into a common (abstract) super-class of those two superclasses (which would then be the classes you use, no more need for ClassA and ClassB) Commented Sep 8, 2020 at 20:47
  • If the three methods are the only public methods of the base classes, you could use composition instead of inheritance. Commented Sep 8, 2020 at 21:31

1 Answer 1

3

This look a lot like a typical case to use class mixin:

class BuildMixin:
    def __init_(self, args):
        super().__init__(args)
        self._private = OtherClass()

    def prepare(self, args):
        super().prepare(args)
        self._private.prepare()

    def setup(self, args):
        super().setup(args)
        self._private.setup(self._smth, args)

    def teardown(self):
        self._private.teardown()
        super().teardown()

class ClassA(BuildMixin, Base):
    pass

class ClassB(BuildMixin, ExtendedBase):
    pass

For illustration purposes here are a bunch of dummy classes that follow your example and use mixin. From your question it is not clear what OtherClass should be, but it seems it also has the prepare, teardown and setup methods:

class OtherClass:
    def prepare(self,arg):
        print('OC perparing')
    def teardown(self):
        print('OC tearing down')
    def setup(self, smth, args):
        print('OC setting up')

class Base:
    def __init__(self, args):
        print('Base init')
        self._smth=args
    def prepare(self,arg):
        print('Base perparing')
    def teardown(self,):
        print('base tearing down')
    def setup(self,args):
        print('base setting up')

class ExtendedBase:
    def __init__(self, args):
        print('ExtBase init')
        self._smth=args
    def prepare(self, arg):
        print('ExtBase perparing')
    def teardown(self):
        print('ExtBase tearing down')
    def setup(self, arg):
        print('ExtBase setting up')

class BuildMixin:
    def __init__(self, arg):
        super().__init__(arg)
        self._private = OtherClass()
    def prepare(self, args):
        super().prepare(args)
        self._private.prepare(args)

    def setup(self, args):
        super().setup(args)
        self._private.setup(self._smth, args)

    def teardown(self):
        self._private.teardown()
        super().teardown()

class  ClassA(BuildMixin, Base):
    pass

class ClassB(BuildMixin, ExtendedBase):
    pass
    
a = ClassA(1)
# prints:
# Base init
b = ClassB(1)
# prints:
# ExtBase init
a.prepare(1)
# prints:
# Base perparing
# OC perparing
b.prepare(1)
# prints:
# ExtBase perparing
# OC perparing

# and so on...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.