Phase: 5. OOP | Estimated time: 2 hours | Milestone Project: No
- Modules 031-040 (all function concepts, generators, iterators)
By the end of this module, you will be able to:
- Explain what OOP is and how it differs from procedural programming
- Define a class as a blueprint and an object as an instance
- Write a simple class definition using
class ClassName: - Create instances of a class
- Describe the purpose of
__init__andself - Understand why OOP helps organize code
Object-Oriented Programming is a paradigm shift. Instead of writing functions that operate on data, you bundle data and behavior together into objects. This makes code more modular, reusable, and easier to reason about as programs grow.
Procedural programming organizes code around functions that operate on data. OOP organizes code around objects that contain both data and behavior.
Procedural: data → function → result
Object: object.data + object.method() → result
A class is a blueprint. An object (or instance) is a concrete thing built from that blueprint.
┌─────────────────────┐
│ Class │ ← Blueprint / Template
│ (Dog) │
│ │
│ Attributes: │
│ - name │
│ - breed │
│ │
│ Methods: │
│ - bark() │
│ - fetch() │
└─────────────────────┘
│
│ instantiate
▼
┌─────────────────────┐ ┌─────────────────────┐
│ Object (instance) │ │ Object (instance) │
│ fido = Dog() │ │ spot = Dog() │
│ fido.name = "Fido" │ │ spot.name = "Spot" │
│ fido.breed = "Lab" │ │ spot.breed = "Pug" │
└─────────────────────┘ └─────────────────────┘
class Dog:
"""A simple Dog class."""
def __init__(self, name):
"""Initialize dog with a name."""
self.name = name
def bark(self):
"""Make the dog bark."""
print(f"{self.name} says Woof!")
fido = Dog("Fido")
fido.bark() # Fido says Woof!__init__is the constructor method — it runs automatically when you create an instance.selfrefers to the current instance. It's how methods access the instance's own data.
- Encapsulation: bundle data + methods together
- Reusability: define once, create many instances
- Organization: group related code logically
- Modeling: match real-world entities directly in code
- Forgetting
self: All instance methods must haveselfas the first parameter. - Calling class methods without instantiation: You must create an instance first.
- Thinking
selfis a keyword: It's a convention — you could name it anything, but never do. - Confusing class vs instance: The class is the blueprint; the instance is the actual object.
- Define a
class Carwith an__init__that takesmake,model, andyear. - Add a method
describe()that prints "This is a [year] [make] [model]". - Create two Car instances with different values.
- Create a class without
__init__and add attributes manually — observe how it works.
- OOP bundles data and behavior into objects.
- A class is a blueprint; an object is an instance of that blueprint.
__init__initializes new objects;selfreferences the current instance.- OOP helps organize and scale code as projects grow.
Continue to Module 042: Attributes and Methods.