Skip to content

Instantly share code, notes, and snippets.

@tmitzka
Created March 14, 2018 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmitzka/8d0ae3e814c371622ec7063193e7fb70 to your computer and use it in GitHub Desktop.
Save tmitzka/8d0ae3e814c371622ec7063193e7fb70 to your computer and use it in GitHub Desktop.
Getting to grips with classes and inheritance
class Cat():
"""A prototype model for a cat."""
def __init__(self, name, age, eye_color, weight_kg = 3):
"""Initialize attributes."""
self.name = name
self.age = age
self.eye_color = eye_color
self.weight_kg = weight_kg
print(f"Welcome, {self.name}!")
def meow(self):
"""Simulate a cat meowing."""
print(f"\n{self.name.title()} just meowed. What's wrong, buddy?")
def scratch(self):
"""Simulate a cat scratching someone."""
print(f"\n{self.name.title()} just scratched me. Ow, stop it!")
class CyberCat(Cat):
"""A prototype model for a cybernetically enhanced cat."""
def __init__(self, name, age, eye_color, weight_kg = 10):
"""Initialize attributes."""
super().__init__(name, age, eye_color, weight_kg)
print("You look ... different.")
def shoot_lasers(self):
"""Simulate laser beams shooting from the cyber cat's eyes."""
print("\nBZZZZZZZRT!!")
print(f"Deadly lasers shoot from the cat's {self.eye_color} eyes.")
def scratch(self):
"""Simulate a cyber cat scratching someone."""
super().scratch()
print("My ... my arm is gone. I think I need a doctor.")
mr_jinx = CyberCat("Mr. Jinx", 6, "green")
mr_jinx.meow()
mr_jinx.shoot_lasers()
mr_jinx.scratch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment