One can get the current base class with super().__thisclass__
Using this solution we can write the below code which meets these requirements:
- Class B inherits from A
- The code in both data methods must be the same and should not include a literal class reference like A or B
class A:
_data = "A data"
@classmethod
def data(cls):
super_instance = super()
this_cls = super_instance.__thisclass__
try:
other_data = super_instance.data()
except AttributeError:
return [this_cls._data]
return [this_cls._data] + other_data
class B(A):
_data = "B data"
@classmethod
def data(cls):
super_instance = super()
this_cls = super_instance.__thisclass__
try:
other_data = super_instance.data()
except AttributeError:
return [this_cls._data]
return [this_cls._data] + other_data
B.data()
# ['B data', 'A data']
Or one can refactor that into the shorter:
class DataGatherer:
@staticmethod
def gather_data(super_instance):
this_cls = super_instance.__thisclass__
try:
other_data = super_instance.data()
except AttributeError:
return [this_cls._data]
return [this_cls._data] + other_data
class A(DataGatherer):
_data = "A data"
@classmethod
def data(cls):
return cls.gather_data(super())
class B(A):
_data = "B data"
@classmethod
def data(cls):
return cls.gather_data(super())