OOP in Python
Object-Oriented Programming (OOP) is a way of organizing code by creating "objects" that represent real-world things.
Classes and Objects
A Class is a blueprint, and an Object is an instance of that blueprint.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
return f"{self.name} says Woof!"
my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.bark())
Inheritance
Inheritance allows a class to take on the attributes and methods of another class.
class Puppy(Dog):
def play(self):
return f"{self.name} is playing!"
little_buddy = Puppy("Pip", "Poodle")
print(little_buddy.bark())
print(little_buddy.play())