object-oriented programming (OOP)
Object-oriented programming (OOP) is a programming paradigm that organizes code around objects, combining data and behavior in a way that mirrors real-world entities. An object is a self-contained component with properties (data attributes) and behavior (methods).
OOP can help to model real-world concepts in code. For example, in the real world, a car is an object that has:
- Properties (color, brand, year)
- Behaviors it can perform (start, accelerate, brake)
In OOP, you structure code similarly:
- Objects bundle related data and functions.
- Properties store details about the object (like a car’s color).
- Methods define what the object can do (like accelerating).
OOP relies on classes, which act as blueprints for creating objects. This approach improves code reuse, scalability, and maintainability by supporting key principles:
- Encapsulation: Hides internal details and exposes only what’s necessary.
- Inheritance: Allows new classes to extend existing ones.
- Polymorphism: Enables different objects to share the same interface.
- Modularity: Organizes code into manageable, logically grouped, and independent parts.
Think of OOP like a car factory—each car follows the same blueprint but can have different colors and features.
Example
Here’s a minimal Car
class that mimics a real-world car:
cars.py
class Car:
def __init__(self, color, brand, year):
# Properties
self.color = color
self.brand = brand
self.year = year
self.is_running = False
# Methods
def start(self):
self.is_running = True
def accelerate(self):
print("Accelerating...")
def brake(self):
print("Braking...")
You can now use this class to create multiple instances of Car
:
>>> from cars import Car
>>> toyota = Car("red", "Toyota", 2010)
>>> toyota.start()
>>> toyota.is_running
True
>>> toyota.accelerate()
Accelerating...
>>> toyota.brake()
Braking...
>>> ford = Car("blue", "Ford", 2015)
>>> print(f"Readying the {ford.color} {ford.brand}.")
Readying the blue Ford.
>>> ford.start()
>>> ford.accelerate()
Accelerating...
This example demonstrates how you can use the Car
class to create multiple objects. Each instance has the same attributes and methods but different values based on the provided arguments.
Related Resources
Learning Path
Object-Oriented Programming (OOP)
Dive into Python OOP! Learn everything from basic classes to advanced topics like using super(), data classes, and design patterns. Enhance your coding with magic methods, managed attributes, and SOLID principles and start building robust, scalable applications today.
For additional information on related topics, take a look at the following resources:
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Inheritance and Composition: A Python OOP Guide (Tutorial)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Inheritance and Composition: A Python OOP Guide (Course)
- Inheritance and Composition: A Python OOP Guide (Quiz)