Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active June 5, 2019 06:15
Show Gist options
  • Save brydavis/597d598266e47864ebe887dba3e00262 to your computer and use it in GitHub Desktop.
Save brydavis/597d598266e47864ebe887dba3e00262 to your computer and use it in GitHub Desktop.
`Unicorn != Pegasus`
# Let's create a unicorn
class Animal:
def __init__(self):
pass
def breathe(self):
print("I am breathing")
def eat(self):
print("I am eating")
def sleep(self):
print("I am sleeping")
class Bird(Animal):
# def __init__(self):
# pass
def fly(self):
print("I am flying")
def lay_eggs(self, n=6):
print("laying eggs:", n)
def eat(self):
print("I am pecking at food")
class Horse(Animal):
def __init__(self):
pass
def gallop(self):
print("I am galloping")
def eat(self):
print("I am chomping on food")
def give_birth(self, n=1):
print("Giving birth to", n)
class Unicorn(Bird, Horse):
def __init__(self):
pass
def be_magical(self):
print("I am magical")
def lay_eggs(self, *args):
pass
animal = Animal()
animal.eat()
animal.breathe()
animal.sleep()
bird = Bird()
bird.eat()
bird.breathe()
bird.sleep()
bird.fly()
bird.lay_eggs(5)
horse = Horse()
horse.eat()
horse.breathe()
horse.sleep()
horse.gallop()
horse.give_birth(2)
unicorn = Unicorn()
unicorn.eat()
unicorn.breathe()
unicorn.sleep()
unicorn.fly()
unicorn.gallop()
unicorn.be_magical()
unicorn.give_birth(3)
# DIAMOND PROBLEM
# Will default to whatever inheritance comes first (MRO)
# You can override by calling parent class and passing self (and other parameters)
unicorn.lay_eggs(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment