Classes and Inheritance
Classes and Inheritance is a reusable Python snippet. Object-oriented programming in Python with classes, inheritance, and magic methods. The full implementation: # Basic class class Dog: # Class variable species = "Canis familiaris" def __init__(self, name, age): self.name = name # Instance variable self.age = age def bark(self): return f"{self.name} says woof!" def __str__(self): return f"{self.name} ({self.age})" # Inheritance class Puppy(Dog): def __init__(self, name, age, toy): super().__init__(name, age) self.toy = toy def bark(self): # Override return f"{self.name} says yip!" # Usage dog = Dog("Buddy", 3) dog.bark() # "Buddy says woof!" puppy = Puppy("Max", 0.5, "ball") puppy.bark() # "Max says yip!" It belongs to the Built-in Functions area of the standard library. This snippet uses only the Python standard library with no third-party dependencies, so it runs on any modern Python 3 interpreter. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.