Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple Inheritance example using Python 3. The program requires pyttsx3. Install it using pip install pyttsx3.
from speaker import Speaker
class Animal:
def eat(self):
print("It eats.")
def sleep(self):
print("It sleeps.")
class Bird(Animal):
def fly(self):
print("It flies in the sky.")
def sing(self):
print("It sings.")
def speak(self, text = "No sound is found."):
print("It can not speak but can write it.")
print(text)
class SingingBird(Speaker, Bird):
def __init__(self, birdName = "Bird Name"):
self.birdName = birdName
def printBirdName(self):
print("This is a {birdName}!".format(birdName = self.birdName))
print(SingingBird.__bases__)
duck = SingingBird("Duck")
duck.printBirdName()
duck.eat()
duck.sleep()
duck.fly()
duck.sing()
duck.speak("Quack quack, quack quack, quack quack")
import pyttsx3
class Speaker:
engine = pyttsx3.init()
def speak(self, text = "No sound is found."):
print(text)
self.engine.say(text)
self.engine.runAndWait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment