Assume I have two classes like Mp3Player, and DVDPlayer and I am going to create a new class MultiPlayer which inherits from both former classes.
Mp3Player and DVDPlayer both have a method with same signature:
class MP3Player:
def play(self, content):
print(f'MP3 player is playing {content}')
class DVDPlayer:
def play(self, content):
print(f'DVD player is playing {content}')
I want to override the play method in MultiPlayer and I want to be able to call the appropriate super class, based on some conditions.
class MultiPlayer(MP3Player, DVDPlayer):
def play(self, content):
if mp3_condition:
# some how call the play method in MP3Player
elif dvd_condition:
# some how call the play method in DVDPlayer
else:
print('the given content is not supported')
I cannot use super().play(content) as based on MRO rules it always resolves to play method in MP3Player.
What is the pythonic way of doing such thing?
MultiPlayerhas internal attributes which are instances ofMP3PlayerandDVDPlayer.