I have a number of Python classes, Say, Class1
, Class2
, Class3
etc from a library/package. I want to extend all of the classes with some common functionalities. If I extend each class individually, we introduce a lot of redundancy and break the "Don't Repeat Yourself" acronym. So, my thought is to have a Base
class and use it extend other classes. For example:
class Base:
def __init__(self):
# I want self.base_attr_1, self.base_attr_2 and so on...
def base_method_1(self, *args, **kwargs):
pass
def base_method_2(self, *args, **kwargs):
pass
# and so on...
So we can extend Class1
, Class2
and so on using maybe Multiple inheritances. Say,
class Class1(Class1, Base):
pass
class Class2(Class2, Base):
pass
# and so on...
So that at last when I create an object of Class1
, Class2
etc., I can use the Base
class attributes and methods. Like;
class_1 = Class1(*args, **kwargs)
print(class_1.base_attr_1)
print(class_1.base_attr_2)
class_1.base_method_1(*args, **kwargs)
class_2.base_method_2(*args, **kwargs)
# and so on..
Please explain how to implement the Class1
, Class2
etc, to extend the Base
class.
Any help is highly appreciated. Thank you.