Object Oriented Programming

Model the real world as objects that bundle data with behavior. It is the most widely used programming paradigm in existence, and also the source of heated debates that have never been resolved and never will be.

animal.py
Encapsulation Inheritance Polymorphism Abstraction
class Animal:
  def __init__(self, name):
    self._name = name # encapsulated
 
  def speak(self): # abstract
    raise NotImplementedError
 
  def name(self): # property
    return self._name
class Dog(Animal): # inherits
  def speak(self): # overrides
    return "Woof!"
 
class Cat(Animal):
  def speak(self): # polymorphic
    return "Meow!"
 
# Dog().speak() → "Woof!"