Lesson 10: Introduction to Object-Oriented Programming (OOP)
Introduction to Object-Oriented Programming (OOP) in Python
So far, we have been writing code that runs step-by-step, using variables to store data and functions to perform actions. This is called procedural programming. It works great for small scripts, but as your programs get larger, keeping track of which functions modify which variables can become a chaotic mess.
Object-Oriented Programming (OOP) is a completely different way to think about your code. Instead of writing a loose collection of variables and functions, you group related data and actions together into single, organized units called Objects.
Think of OOP as modeling your code after the real world. A real-world object (like a car) has characteristics (color, brand, top speed) and behaviors (accelerate, brake, honk). OOP allows you to build virtual versions of these objects.
1. Classes vs. Objects (The Blueprint and the Building)
To understand OOP, you must understand the difference between a Class and an Object.
- The Class (The Blueprint): A class is a template or a set of instructions. It does not actually exist as a usable thing in your program yet. A blueprint of a house tells you how many doors and windows it has, but you cannot live in a blueprint.
- The Object (The Instance): An object is the actual, tangible thing built from the class blueprint. Once you use the blueprint to build a house, that physical house is the object. You can build a hundred different houses (objects) from the same blueprint (class).
2. Attributes and Methods (Data and Actions)
When you create a class, you define two main things:
- Attributes: These are variables that belong to the object. They store data about the object’s current state (e.g.,
color = "red",fuel_level = 100). - Methods: These are simply functions that belong to the object. They define what the object can do (e.g.,
start_engine(),refuel()).
3. The __init__ Method (The Constructor)
When you create a new object from a class, Python needs to know its initial starting state. You do this using a special, built-in method called __init__ (short for initialize).
This function runs automatically the exact moment a new object is created.
The self Keyword: Inside any class method, you will see a parameter called self. This is how the object refers to itself. When the object wants to access its own attributes, it uses self.attribute_name.
See it in Action
Let’s build a simple Car class to see how attributes, methods, and objects all work together in Python.
# 1. Defining the Class (The Blueprint)
class Car:
# 2. The __init__ method sets up the initial attributes
def __init__(self, brand, color):
self.brand = brand
self.color = color
self.is_running = False # A default attribute
# 3. Defining a Method (An action the car can take)
def start_engine(self):
self.is_running = True
print("The " + self.color + " " + self.brand + " engine is now running. Vroom!")
def stop_engine(self):
self.is_running = False
print("The engine is off.")
# 4. Creating Objects (Building actual cars from the blueprint)
car_one = Car("Toyota", "Blue")
car_two = Car("Ferrari", "Red")
# 5. Accessing attributes and calling methods
print(car_one.brand) # Prints: Toyota
# The cars act independently of each other
car_two.start_engine()
# Prints: The Red Ferrari engine is now running. Vroom!
print(car_two.is_running) # Prints: True
print(car_one.is_running) # Prints: False