Introduction to Object Oriented Concepts
Object Oriented Programming (OOP) is a way of structuring code around objects - self-contained units that bundle together data (attributes) and behavior (methods) that belong together, modeled closely on real-world entities.
Classes and Objects
A class is a blueprint that defines what attributes and methods its objects will have. An object is a specific instance created from that class, with its own actual data.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def introduce(self):
print(f"Hi, I'm {self.name}, studying {self.course}")
s1 = Student("Abhay", "Data Science")
s1.introduce()
Here, Student is the class, and s1 is an object (an instance) of that class, holding its own unique data.
Why Use OOP?
- Organizes related data and behavior together, making large programs easier to manage
- Models real-world entities naturally, making code more intuitive to design and read
- Encourages code reuse through inheritance, reducing duplication
- Used internally by nearly every major Python library, including Pandas and Scikit-learn
Every time you create a Pandas DataFrame or a Scikit-learn model, you're actually creating an object - which is why a solid grasp of OOP makes these libraries far less mysterious.
Coming Up Next
Next, you'll look at built-in class attributes - special attributes that Python automatically provides for every class.