🧱 Chapter 10 – Object Oriented Programming (OOP) in Python
📘 1. What is a Class?
class Employee: # 🏗️ This creates a new class blueprint
name = "Prathamesh" # 🏷️ Class attribute
age = 17
salary = 10000000
print(Employee.name, Employee.age, Employee.salary)
🧠 Concept:
- A class is a blueprint or template for creating objects.
- Variables like
name
, age
, and salary
inside the class (outside functions) are called class attributes.
- These attributes are shared among all instances unless overridden.
🧍♂️ 2. Class vs Instance Attributes
class Employee:
age = 17
salary = 10000000 # 🎯 Class Attribute
prathamesh = Employee()
prathamesh.salary = 12000000 # 🧍 Instance Attribute
print(Employee.age, Employee.salary)
🧠 Concept:
- Class Attributes: Belong to the class itself (shared across all objects).
- Instance Attributes: Unique to each object (created using
object.attribute = value
).
- 🔁 If both exist, instance attribute overrides class attribute during access.
🧠 3. The self
Keyword – Instance Method
class Employee:
age = 17
salary = 10000000 # 🎯 Class Attribute
def getInfo(self): # 👀 'self' refers to current object
print(f"The age is {self.age} and salary is {self.salary}")
@staticmethod
def greet(): # 🚫 Doesn't need 'self', doesn't touch object data
print("Good Morning")
prathamesh = Employee()
prathamesh.salary = 12000000
Employee.greet() # Static method (no object data used)
Employee.getInfo(prathamesh) # Equivalent to prathamesh.getInfo()
🧠 Concept:
self
gives access to the object's attributes/methods inside class functions.