Object-Oriented Programming in Python - A Lecture Note
Object-Oriented Programming in Python - A Lecture Note
Learning Objectives
- Understand the principles of OOP
Theoretical Background
OOP helps in structuring a program into simple, reusable pieces of code. Key concepts
include:
- Inheritance
- Encapsulation
- Polymorphism
Example Code
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
def speak(self):
return f"{self.name} barks."
d = Dog("Buddy")
print(d.speak())
Summary
OOP allows for cleaner code architecture and reuse. Python’s dynamic typing makes OOP
flexible.
Review Questions
- What is inheritance?