Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects that contain both data (attributes) and behavior (methods). Python’s OOP features make it easy to write maintainable, reusable code.Classes and Objects
Defining a Class
A class is a blueprint for creating objects. Let’s look at a real example from a contact management system:The
__init__ method is a special method called a constructor. It runs automatically when you create a new instance of the class.Creating Objects
Once you have a class, you can create instances (objects) of it:Encapsulation
Private Attributes
In Python, attributes prefixed with an underscore (_) are considered private by convention:
- Private Attributes
- Why Encapsulation?
Properties (Getters and Setters)
Python uses the@property decorator to create getters and setters:
Type Hints: The
-> str syntax indicates the return type of the method. While optional, type hints improve code readability and enable better IDE support.Special Methods (Dunder Methods)
Special methods (also called magic methods or dunder methods) allow you to define how objects behave with built-in Python operations:__str__ and __repr__
__str__ and __repr__
These methods control how objects are converted to strings:
Common Special Methods
Common Special Methods
| Method | Purpose | Example |
|---|---|---|
__init__ | Constructor | obj = MyClass() |
__str__ | String representation | str(obj), print(obj) |
__repr__ | Developer representation | repr(obj) |
__len__ | Length | len(obj) |
__eq__ | Equality | obj1 == obj2 |
__lt__ | Less than | obj1 < obj2 |
__add__ | Addition | obj1 + obj2 |
Instance Methods vs Static Methods
- Instance Methods
- Static Methods
Instance methods operate on an instance of the class and have access to
self:Complex Classes: Contact Manager
Let’s look at a more complex class that manages a collection of contacts:Private Methods: Methods prefixed with
_ (like _cargar_contactos) are internal helper methods not meant to be called from outside the class.CRUD Operations
The ContactManager class implements Create, Read, Update, and Delete operations:Create: Adding Contacts
Create: Adding Contacts
Read: Searching Contacts
Read: Searching Contacts
Update: Editing Contacts
Update: Editing Contacts
Delete: Removing Contacts
Delete: Removing Contacts
OOP Best Practices
Single Responsibility
Each class should have one clear purpose.
Encapsulation
Keep attributes private and use properties for controlled access.
Use Type Hints
Type hints improve code readability and IDE support.
Document Your Classes
Use docstrings to explain class purpose and methods.
Real-World Example
Here’s how these classes work together in a real application:Practice Exercises
Exercise 1: Create a Book Class
Exercise 1: Create a Book Class
Create a
Book class with:- Attributes: title, author, isbn, price
- Properties with validation
- Methods:
to_dict(),from_dict(),apply_discount(percentage) - Special methods:
__str__and__repr__
Exercise 2: Library Manager
Exercise 2: Library Manager
Create a
LibraryManager class that:- Manages a collection of books
- Can add, remove, and search books
- Saves/loads books to/from JSON
- Calculates total library value
Exercise 3: Inheritance
Exercise 3: Inheritance
Extend the Contact class to create:
BusinessContact(with company and job title)PersonalContact(with birthday and relationship)
Contact and add their specific attributes.Next Steps
Python Projects
Apply OOP concepts in real-world projects
Python Basics
Review Python fundamentals