I need to define a number of classes that have some common data and behavior. To avoid code duplication, I inherit all of them from a base class and put all common stuff into the base class. The base class itself inherits from a class that is part of an external package I use.
The base class will not represent any real object, it is just a collection of what is common between its children. So I want to make the base class abstract. I have to define the MergeMetas class below because the external class Transport itself has an abstract class defives from a metaclass (different from ABC).
This is what I came up with (not my real classes of course, just to demonstrate):
from some_external_package import Transport
from abc import ABC, abstractmethod
class MergeMetas(type(ABC), type(Transport)): pass
class UnrealCar(Transport, metaclass=MergeMetas):
@abstractmethod
def __init__(self, a, b, c):
# process a, b and c
@abstractmethod
def do_something(self):
# do some common stuff
@abstractmethod
def do_something_specific(self):
"""
All child classes have to implement this method but there's nothing in common
"""
raise NotImplementedError
class Volvo(UnrealCar):
def __init__(self, a, b, c):
super().__init__(a, b, c)
def do_something(self):
super().do_something()
# do some specific staff
def do_something_specific(self):
# do some really specific staff
class Opel(UnrealCar):
def __init__(self, a, b, c, d):
super().__init__(a, b, c)
# proceed d
def do_something(self):
super().do_something()
# do some specific stuff
def do_something_specific(self):
# do some really specific stuff
Is my approach correct for the problem stated in the first part?
The __init__
method will have several parameters, some of them will be common for all children, others can be specific. Common parameters will be passed to the base class' __init__
. Knowing this, do I have to repeat arguments a, b, and c each time or rather go for *args and **kwargs? I read in some places using keyword args is not advised.