Browse Source

added FlacFile class as example for duck typing instead of polymorpism using inhiterance

master
T. Meissner 10 years ago
parent
commit
8671e2e9dd
1 changed files with 13 additions and 1 deletions
  1. +13
    -1
      python_3_oop/chapter03/audiofile.py

+ 13
- 1
python_3_oop/chapter03/audiofile.py View File

@ -1,4 +1,5 @@
# small example for inhiterance with polymorphism
# and duck typing as mostly better alternative
class AudioFile:
@ -29,4 +30,15 @@ class OggFile(AudioFile):
ext = "ogg"
def play(self):
print("Playing {} as ogg".format(self.filename))
print("Playing {} as ogg".format(self.filename))
class FlacFile:
def __init__(self, filename):
if not filename.endswith(".flac"):
raise Exception("Invalid file format")
self.filename = filename
def play(self):
print("Playing {} as flac".format(self.filename))

Loading…
Cancel
Save