This week introduces Object-Oriented Programming (OOP) — the foundation of most modern software. OOP helps you:
- Organize code in a scalable, reusable way.
- Build systems that model real-world objects and behaviors.
- Understand the principles behind major frameworks (Flask, Django, SQLAlchemy, etc.)
Mastering OOP is essential for writing maintainable Python code and succeeding in professional software environments.
Define a class
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} says woof!")Create an object
fido = Dog("Fido")
fido.bark() # Fido says woof!__init__is the constructor method.selfrefers to the instance of the object.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age“Dunder” = double underscore (e.g., __str__, __repr__, __len__)
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
def __str__(self):
return f"{self.name} costs ${self.price}"Base and Derived Classes
class Animal:
def speak(self):
print("Some sound")
class Cat(Animal):
def speak(self):
print("Meow")
kitty = Cat()
kitty.speak() # Meow- Overriding: Redefine methods in a child class.
- Composition: Use other classes as part of your class.
class Engine:
def start(self):
print("Engine starting...")
class Car:
def __init__(self):
self.engine = Engine()
def drive(self):
self.engine.start()
print("Car is moving")-
What does
__init__()do in a class? -
What keyword is used to inherit from a parent class?
-
What does the
selfkeyword represent? -
Which dunder method controls how an object is printed?
-
True or False: You can have multiple instances of the same class.
-
What is method overriding?
Instructions:
Create a class called Student that stores name and grade and prints them when an instance is created.
Learning Objective: Understand constructors and instance variables.
# TODO: Create a class called `Student` that stores `name` and `grade` and prints them when an instance is created.Instructions:
Extend your Student class with a method that increases the grade.
Learning Objective: Practice adding behavior (methods) to your class.
# TODO: Extend your `Student` class with a method that increases the grade.Instructions:
Add a __str__ method to your class so the student prints nicely.
Learning Objective: Use dunder methods to control object representation.
# TODO: Add a `__str__` method to your class so the student prints nicely.Instructions:
Create a Teacher class that inherits from Person, but overrides the introduce() method.
Learning Objective: Learn basic inheritance and method overriding.
# TODO: Create a `Teacher` class that inherits from `Person`, but overrides the `introduce()` method.Instructions:
Create two classes: Battery and Phone, where Phone uses a Battery.
Learning Objective: Understand class composition by combining behaviors.
# TODO: Create two classes: `Battery` and `Phone`, where `Phone` uses a `Battery`.Instructions:
Create a list of Dog objects and make them bark.
Learning Objective: Use multiple instances of a class in a loop.
# TODO: Create a list of `Dog` objects and make them bark.- You learned how to define and instantiate classes.
- You wrote constructors and added methods to encapsulate object behavior.
- You used
__str__to customize how your objects display. - You applied inheritance to reuse and override behavior.
- You practiced composition to combine class functionality.
OOP gives you structure and reuse — a critical step toward building larger applications, frameworks, and systems.
- https://codingnomads.com/course/python-programming-301
- Section 1) Introduction & Prerequisites
- Section 2) Classes, Objects, and Methods
- Section 3) Inheritance
First:
- Install the Python 301 Lab Exercises
Then, complete the following exercises -- you can, of course, do them all:
- 02_classes-objects-methods
- 02_02_planets.py
- 02_03_car.py
- 02_04_classy_shapes.py
- 02_05_smaller-amount.py
- 03_inheritance
- 03_01_more_food.py
- 03_02_inheritance.py