OOP Basics
Complete learning notes
1. Introduction
So far, we've written code as a series of steps and functions. Object-Oriented Programming (OOP) offers a different way of organizing code — one that models real-world "things" as objects with their own data and behavior. Many ML libraries (like Scikit-learn) are built using OOP — every model you create, such as LinearRegression(), is actually an object. Understanding OOP basics helps you read, use, and eventually build such libraries confidently.
2. What is OOP?
Simple definition: Object-Oriented Programming is a way of writing code by creating "objects" that bundle together data (attributes) and behavior (methods) related to a single real-world concept.
Technical explanation: OOP is a programming paradigm where a class acts as a blueprint defining attributes and methods, and an object is a specific instance created from that blueprint, holding its own data.
3. Why is it Important?
- Most professional Python libraries, including ML libraries, are organized using classes and objects.
- OOP helps model real-world entities (like a "Student," "Car," or "Model") in a natural, organized way.
- It makes large codebases easier to maintain, extend, and reuse.
4. Prerequisites
You should be comfortable with variables, data types, and functions (Topics 2 and 3).
5. Core Concepts
- Class
- Object (instance)
- Attributes (data)
- Methods (behavior)
- The
__init__constructor - The
selfkeyword - The four pillars of OOP (introduced conceptually): Encapsulation, Inheritance, Polymorphism, Abstraction
6. Detailed Explanation
a) Class
A class is a blueprint or template. It defines what attributes and methods every object created from it will have — but a class itself is not usable data; it's just the design.
b) Object
An object is a real, usable instance created from a class. You can create many different objects from the same class, each with its own separate data.
In simple words: a class is like a cookie cutter; objects are the actual cookies made using it — same shape (structure), but each cookie can be decorated differently (different data).
c) Attributes
Attributes are variables that belong to an object, representing its data or state (e.g., a student's name and age).
d) Methods
Methods are functions defined inside a class that describe what an object can do (e.g., a student object might have a study() method).
e) The `__init__` Constructor
__init__ is a special method automatically called when a new object is created. It's typically used to set up (initialize) the object's starting attributes.
f) The `self` Keyword
self refers to the specific object currently being worked with. It lets a method access and modify that particular object's own attributes, distinguishing it from other objects of the same class.
g) The Four Pillars of OOP (Introduced Conceptually)
- Encapsulation: Bundling data and the methods that work on that data together inside one class.
- Inheritance: Allowing one class to reuse and extend the attributes and methods of another class.
- Polymorphism: Allowing the same method name to behave differently depending on the object calling it.
- Abstraction: Hiding complex internal details and exposing only what's necessary to use an object.
(These four pillars are introduced here at a conceptual level; you will see them applied more deeply as you progress.)
7. How It Works
- You define a class using the
classkeyword, listing its attributes and methods. - When you create an object from the class (e.g.,
Student("Riya", 20)), Python automatically calls__init__. __init__sets up that object's own attributes using the values you passed in.selfinside any method always refers back to that specific object, so each object keeps its own separate data.- You can then call methods on the object, and they will operate using that object's own attribute values.
8. Real-World Example
Think of a "Car" blueprint (class). Every car built from that blueprint (object) shares the same design — four wheels, an engine, a steering wheel — but each individual car (object) has its own color, number plate, and mileage (attributes). Pressing the accelerator (a method) works the same way conceptually across all cars, but affects only that specific car's speed.
9. Technical Example
pythonclass Student: def __init__(self, name, age): self.name = name self.age = age def introduce(self): print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
Here, Student is the class (blueprint), __init__ sets up each new student's name and age, and introduce is a method that uses self to access that particular student's own data.
10. Python Example
python# Defining a class class Student: def __init__(self, name, age, marks): self.name = name self.age = age self.marks = marks def introduce(self): print(f"Hi, I'm {self.name}, age {self.age}.") def has_passed(self): return self.marks >= 40 # Creating objects (instances) from the class student1 = Student("Aarav", 21, 78) student2 = Student("Meera", 20, 35) # Calling methods on each object student1.introduce() student2.introduce() print(student1.name, "passed?", student1.has_passed()) print(student2.name, "passed?", student2.has_passed()) # Each object keeps its own separate data print("Student 1 marks:", student1.marks) print("Student 2 marks:", student2.marks)
Expected Output:
textHi, I'm Aarav, age 21. Hi, I'm Meera, age 20. Aarav passed? True Meera passed? False Student 1 marks: 78 Student 2 marks: 35
11. Code Explanation
class Student:begins the class definition — the blueprint for all student objects.def __init__(self, name, age, marks):is the constructor; it runs automatically whenever a newStudentobject is created, storing the given values as that object's own attributes usingself.name,self.age, andself.marks.student1 = Student("Aarav", 21, 78)creates an actual object, passing values into__init__.student1.introduce()calls theintroducemethod onstudent1specifically — inside the method,selfrefers tostudent1, so it printsstudent1's own name and age.has_passed()usesself.marksto check that specific object's marks, which is whystudent1andstudent2can give different results from the very same method.- Notice how
student1.marksandstudent2.marksremain completely separate, even though both objects were created from the sameStudentclass.
12. Advantages
- Groups related data and behavior together in one organized unit.
- Makes code more reusable — one class can create many objects.
- Mirrors real-world thinking, making complex systems easier to design and understand.
- Forms the foundation for building and understanding professional Python libraries.
13. Limitations
- Can feel like unnecessary overhead for very small, simple scripts.
- Beginners often find
selfand__init__confusing at first. - Poorly designed classes (too large, doing too much) can become hard to maintain.
14. Common Mistakes
- Forgetting to include
selfas the first parameter in method definitions. - Confusing a class (the blueprint) with an object (an actual instance).
- Trying to access an attribute before it has been set in
__init__. - Forgetting to use
self.when referring to an object's own attribute inside a method.
15. Best Practices
- Name classes using CapitalizedWords (e.g.,
Student,Car) to distinguish them from variables and functions. - Keep each class focused on representing one clear concept.
- Always initialize all necessary attributes inside
__init__. - Use descriptive method names that clearly describe the action being performed.
16. Real-World Applications
- ML libraries like Scikit-learn represent models as objects (e.g., a
LinearRegressionobject storing its own learned parameters). - Representing structured real-world entities in software, such as customers, products, or bank accounts.
- Building reusable components in larger software systems, including data pipelines.
17. Interview-Oriented Points
- Be ready to explain the difference between a class and an object clearly, with an example.
- Understand the purpose of
__init__and when it runs. - Know why
selfis needed and what it refers to. - Be able to name and briefly explain the four pillars of OOP.
18. Exam-Oriented Points
- A class is a blueprint; an object is an instance created from that blueprint.
__init__is the constructor, automatically called when an object is created.selfrefers to the current object and is required as the first parameter in instance methods.- The four pillars of OOP are Encapsulation, Inheritance, Polymorphism, and Abstraction.
19. Comparison Table — Class vs Object
| Aspect | Class | Object |
|---|---|---|
| Definition | A blueprint/template | An actual instance created from the class |
| Existence | Exists only as a design | Exists in memory with real data |
| Quantity | One class definition | Many objects can be created from one class |
| Example | Student (the design) | student1, student2 (actual students) |
20. Quick Revision
- A class is a blueprint; an object is a specific instance created from it.
- Attributes store an object's data; methods define what it can do.
__init__automatically initializes a new object's attributes.selfrefers to the specific object a method is currently working with.- The four OOP pillars: Encapsulation, Inheritance, Polymorphism, Abstraction.