Table of Contents
-
- Introduction to OOP
-
- Key Principles of OOP
-
- Basic Syntax in Python
-
- Defining and Using Classes
-
- Attributes and Methods
-
- The
__init__
Method and Object Instantiation
- The
-
- Encapsulation and Access Control
-
- Inheritance and Subclasses
-
- Polymorphism
-
- Abstraction and Interfaces
-
- Special Methods (Magic/Dunder Methods)
-
- Class and Static Methods
-
- Composition vs. Inheritance
-
- Design Patterns in OOP
-
- Testing and Debugging OOP Code
-
- Best Practices and Tips
-
- Practice Projects and Resources
1. Introduction to OOP
-
- What is OOP?
OOP is a programming paradigm that uses “objects” to model real-world entities. It organizes software design around data, or objects, rather than functions and logic.
- What is OOP?
-
- Why OOP in Python?
Python supports multiple paradigms but OOP helps create reusable, scalable, and maintainable code.
- Why OOP in Python?
2. Key Principles of OOP
-
- Encapsulation: Bundling data (attributes) and methods in one unit (class).
-
- Inheritance: Deriving new classes from existing ones to promote code reuse.
-
- Polymorphism: Methods behaving differently in subclasses.
-
- Abstraction: Hiding complex implementation details and showing only relevant parts.
3. Basic Syntax in Python
-
- Defining a class:
python
class MyClass:
pass
- Defining a class:
-
- Creating an object/instance:
python
obj = MyClass()
- Creating an object/instance:
4. Defining and Using Classes
-
- Define a class with attributes and methods.
python
class Dog:
def bark(self):
print(“Woof!”)my_dog = Dog()
my_dog.bark() # Output: Woof!
- Define a class with attributes and methods.
5. Attributes and Methods
-
- Attributes: Variables inside the class.
-
- Methods: Functions defined inside a class.
python
class Dog:
def init(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says Woof!")
6. The __init__
Method and Object Instantiation
-
- The
__init__
method initializes object attributes.
- The
python
class Dog:
def init(self, name, age):
self.name = name
self.age = age
dog1 = Dog(“Buddy”, 3)
print(dog1.name) # Output: Buddy
7. Encapsulation and Access Control
-
- Use conventions for private attributes: prefix with
_
or__
.
- Use conventions for private attributes: prefix with
python
class Car:
def init(self, speed):
self._speed = speed # protected-like attribute
def get_speed(self):
return self._speed
-
- Use property decorators for getter and setter:
python
class Person:
def init(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
8. Inheritance and Subclasses
-
- Create subclasses that inherit from parent classes.
python
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return “Woof!”
class Cat(Animal):
def speak(self):
return “Meow!”
dog = Dog()
print(dog.speak()) # Woof!
cat = Cat()
print(cat.speak()) # Meow!
9. Polymorphism
-
- Same interface, different implementations.
python
class Bird:
def speak(self):
return “Tweet”
class Parrot(Bird):
def speak(self):
return “Squawk”
def animal_sound(animal):
print(animal.speak())
animal_sound(Bird()) # Tweet
animal_sound(Parrot()) # Squawk
10. Abstraction and Interfaces
-
- Abstract Base Classes (ABC) via
abc
module enforce method implementation.
- Abstract Base Classes (ABC) via
python
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Square(Shape):
def init(self, side):
self.side = side
def area(self):
return self.side ** 2
sq = Square(4)
print(sq.area()) # 16
11. Special Methods (Magic/Dunder Methods)
-
- Customize behavior of objects.
python
class Point:
def init(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"Point({self.x}, {self.y})"
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
p1 = Point(1, 2)
p2 = Point(3, 4)
print(p1 + p2) # Point(4, 6)
12. Class and Static Methods
-
- Use
@classmethod
for methods affecting the class rather than instances.
- Use
-
- Use
@staticmethod
when method doesn’t use class or instance reference.
- Use
python
class Pizza:
def init(self, ingredients):
self.ingredients = ingredients
@classmethod
def margherita(cls):
return cls(['cheese', 'tomatoes'])
@staticmethod
def greet():
print("Welcome to the Pizza store!")
pizza = Pizza.margherita()
print(pizza.ingredients) # [‘cheese’, ‘tomatoes’]
Pizza.greet() # Welcome to the Pizza store!
13. Composition vs. Inheritance
-
- Prefer composition when:
-
- You want to model a “has-a” relationship.
-
- Example:
-
- Prefer composition when:
python
class Engine:
def start(self):
print(“Engine started”)
class Car:
def init(self):
self.engine = Engine()
car = Car()
car.engine.start() # Engine started
14. Design Patterns in OOP
-
- Examples include Singleton, Factory, Observer, Strategy pattern.
-
- Learn these to write efficient and maintainable code.
15. Testing and Debugging OOP Code
-
- Use
unittest
orpytest
to write test cases for classes and methods.
- Use
-
- Debug using breakpoints and print statements.
16. Best Practices and Tips
-
- Follow PEP 8 for code style.
-
- Write docstrings for classes and methods.
-
- Favor composition over inheritance when possible.
-
- Keep classes focused and single-responsibility.
-
- Use meaningful class and method names.
17. Practice Projects and Resources
-
- Projects:
-
- Build a library management system.
-
- Create a banking system with accounts and transactions.
-
- Develop a simple RPG game character system.
-
- Projects:
-
- Resources:
-
- Python’s Official Tutorial: https://docs.python.org/3/tutorial/classes.html
-
- “Python 3 Object-Oriented Programming” by Dusty Phillips
-
- Real Python Tutorials: https://realpython.com/python3-object-oriented-programming/
-
- Resources:
Mastering OOP in Python requires understanding the core concepts and practicing their implementation through real code. Start by learning the syntax, then explore advanced topics such as inheritance, polymorphism, and design patterns. Constantly apply best practices and enhance your skills by building projects that challenge your understanding of OOP.