KEMBAR78
Python: Polymorphism | PPTX
Polymorphism
Damian Gordon
Polymorphism
• Polymorphism simply means that we can call the same method
name with parameters, and depending on the parameters, it
will do different things. For example:
>>> print(6 * 5)
>>> print(“Hello” * 5)
Polymorphism
• Polymorphism simply means that we can call the same method
name with parameters, and depending on the parameters, it
will do different things. For example:
>>> print(6 * 5)
>>> print(“Hello” * 5)
Mult (6, 5)
Mult (“Hello”, 5)
Polymorphism
• Polymorphism simply means that we can call the same method
name with parameters, and depending on the parameters, it
will do different things. For example:
>>> print(6 * 5)
>>> print(“Hello” * 5)
Mult (6, 5)
Mult (“Hello”, 5)
30
HelloHelloHelloHelloHello
Polymorphism
• A more complicated example to consider would be to think
about creating a method called play() to play an audio file.
• A media player will be needed to load the AudioFile object.
• The instruction to play the file might be as simple as:
>>> audio_file.play( )
Polymorphism
• However, different audio files use different compression
algorithms (e.g. .mp3, .wma, .ogg), and some aren’t stored as
compressed at all (e.g. .wav).
• We can use inheritance with polymorphism to simplify the
design. Each filetype is represented as a different subclass of
AudioFile, and each of those has a play( ) method.
Polymorphism
AudioFile
_ _init_ _( )
MP3File
play( )
WAVFile
play( )
OGGFile
play( )
Polymorphism
class AudioFile:
def _ _init_ _(self, filename):
if not filename.endswith(self.ext):
# THEN
raise Exception(“Invalid format”)
# ENDIF;
self.filename = filename
# END init()
# END CLASS.
Polymorphism
class AudioFile:
def _ _init_ _(self, filename):
if not filename.endswith(self.ext):
# THEN
raise Exception(“Invalid format”)
# ENDIF;
self.filename = filename
# END init()
# END CLASS.
Check if the file extension
of the audio being played
is a known extension,
self.ext is set in each
of the subclasses.
Raise an exception if it’s an
unknown file extension
If it’s a known file
extension, then assign the
filename passed in to
self.filename
Polymorphism
class MP3File(AudioFile):
ext = “mp3”
def play(self):
print(“playing {} as mp3”.format(self.filename))
# END play
# END CLASS.
Polymorphism
class WAVFile(AudioFile):
ext = “wav”
def play(self):
print(“playing {} as wav”.format(self.filename))
# END play
# END CLASS.
Polymorphism
class OGGFile(AudioFile):
ext = “ogg”
def play(self):
print(“playing {} as ogg”.format(self.filename))
# END play
# END CLASS.
Polymorphism
• Here’s how we run it:
>>> mp3 = MP3File(“myfile.mp3”)
>>> mp3.play()
playing myfile.mp3 as mp3
Polymorphism
• Here’s another one:
>>> wav = WAVFile(“myfile.wav”)
>>> wav.play()
playing myfile.wav as wav
Polymorphism
• Here’s an error:
>>> ogg_declared_as_mp3 = MP3File("myfile.ogg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "polymorphic_audio.py", line 4, in __init__
raise Exception("Invalid format")
Exception: Invalid format
etc.

Python: Polymorphism

  • 1.
  • 2.
    Polymorphism • Polymorphism simplymeans that we can call the same method name with parameters, and depending on the parameters, it will do different things. For example: >>> print(6 * 5) >>> print(“Hello” * 5)
  • 3.
    Polymorphism • Polymorphism simplymeans that we can call the same method name with parameters, and depending on the parameters, it will do different things. For example: >>> print(6 * 5) >>> print(“Hello” * 5) Mult (6, 5) Mult (“Hello”, 5)
  • 4.
    Polymorphism • Polymorphism simplymeans that we can call the same method name with parameters, and depending on the parameters, it will do different things. For example: >>> print(6 * 5) >>> print(“Hello” * 5) Mult (6, 5) Mult (“Hello”, 5) 30 HelloHelloHelloHelloHello
  • 5.
    Polymorphism • A morecomplicated example to consider would be to think about creating a method called play() to play an audio file. • A media player will be needed to load the AudioFile object. • The instruction to play the file might be as simple as: >>> audio_file.play( )
  • 6.
    Polymorphism • However, differentaudio files use different compression algorithms (e.g. .mp3, .wma, .ogg), and some aren’t stored as compressed at all (e.g. .wav). • We can use inheritance with polymorphism to simplify the design. Each filetype is represented as a different subclass of AudioFile, and each of those has a play( ) method.
  • 7.
    Polymorphism AudioFile _ _init_ _() MP3File play( ) WAVFile play( ) OGGFile play( )
  • 8.
    Polymorphism class AudioFile: def __init_ _(self, filename): if not filename.endswith(self.ext): # THEN raise Exception(“Invalid format”) # ENDIF; self.filename = filename # END init() # END CLASS.
  • 9.
    Polymorphism class AudioFile: def __init_ _(self, filename): if not filename.endswith(self.ext): # THEN raise Exception(“Invalid format”) # ENDIF; self.filename = filename # END init() # END CLASS. Check if the file extension of the audio being played is a known extension, self.ext is set in each of the subclasses. Raise an exception if it’s an unknown file extension If it’s a known file extension, then assign the filename passed in to self.filename
  • 10.
    Polymorphism class MP3File(AudioFile): ext =“mp3” def play(self): print(“playing {} as mp3”.format(self.filename)) # END play # END CLASS.
  • 11.
    Polymorphism class WAVFile(AudioFile): ext =“wav” def play(self): print(“playing {} as wav”.format(self.filename)) # END play # END CLASS.
  • 12.
    Polymorphism class OGGFile(AudioFile): ext =“ogg” def play(self): print(“playing {} as ogg”.format(self.filename)) # END play # END CLASS.
  • 13.
    Polymorphism • Here’s howwe run it: >>> mp3 = MP3File(“myfile.mp3”) >>> mp3.play() playing myfile.mp3 as mp3
  • 14.
    Polymorphism • Here’s anotherone: >>> wav = WAVFile(“myfile.wav”) >>> wav.play() playing myfile.wav as wav
  • 15.
    Polymorphism • Here’s anerror: >>> ogg_declared_as_mp3 = MP3File("myfile.ogg") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "polymorphic_audio.py", line 4, in __init__ raise Exception("Invalid format") Exception: Invalid format
  • 16.