OOP Basics for Data Analysts
You do not need to be an expert in object-oriented programming as a data analyst — but you must understand classes enough to read library code, write reusable analysis pipelines, and pass interviews that include an OOP question.
What is a class and how do you define one?
class DataReport:
def __init__(self, title, author):
self.title = title
self.author = author
self.sections = []
def add_section(self, name):
self.sections.append(name)
def summary(self):
return f"{self.title} by {self.author} — {len(self.sections)} sections"
report = DataReport("Q2 Sales", "Rahul")
report.add_section("Executive Summary")
print(report.summary())__init__ is the constructor — called when you create an instance. self refers to the instance being created. Methods are functions defined inside a class. Classes group related data and behaviour together.
What is the difference between class attributes and instance attributes?
class Analyst:
company = "EVIKA Academy" # class attribute — shared by all instances
def __init__(self, name):
self.name = name # instance attribute — unique per instance
a1 = Analyst("Rahul")
a2 = Analyst("Priya")
print(a1.company) # "EVIKA Academy"
print(a1.name) # "Rahul"
print(a2.name) # "Priya"Class attributes are shared across all instances — change them once and all instances see the change. Instance attributes are set per object. In data pipelines, class attributes hold config (file paths, thresholds) shared across methods.
What are __str__ and __repr__ methods?
class Report:
def __init__(self, title, rows):
self.title = title
self.rows = rows
def __str__(self): # user-friendly string
return f"Report: {self.title} ({self.rows} rows)"
def __repr__(self): # developer-friendly repr
return f"Report(title='{self.title}', rows={self.rows})"
r = Report("Sales Q2", 1500)
print(str(r)) # Report: Sales Q2 (1500 rows)
print(repr(r)) # Report(title='Sales Q2', rows=1500)__str__ is what print() shows; __repr__ is what the Python console shows. Always define __repr__ so objects are debuggable. Pandas DataFrame has a __repr__ that shows the table — this is why print(df) works nicely.
What is inheritance in Python?
class Report:
def __init__(self, title):
self.title = title
def generate(self):
return f"Generating: {self.title}"
class SalesReport(Report):
def __init__(self, title, region):
super().__init__(title) # call parent __init__
self.region = region
def generate(self):
base = super().generate() # call parent method
return f"{base} for {self.region}"
sr = SalesReport("Q2 Sales", "North")
print(sr.generate())Inheritance lets a child class reuse and extend a parent class. super() calls the parent's method. In analytics, you might create a base DataPipeline class and extend it for SalesPipeline, HRPipeline, etc.
How do you use @property and @staticmethod?
class Report:
def __init__(self, data):
self._data = data
@property
def row_count(self): # access like attribute, not method
return len(self._data)
@staticmethod
def validate_format(filepath): # does not need self
return filepath.endswith(".csv") or filepath.endswith(".xlsx")
r = Report([1, 2, 3])
print(r.row_count) # 3 — no parentheses
print(Report.validate_format("data.csv")) # True@property lets you compute values on access — useful for derived attributes like row_count, null_count, memory_usage. @staticmethod is for utility functions that logically belong to the class but do not need instance data.
EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS
Want to master Python with live practice?
Join our Python for Data Analysis course — live classes in Noida and online across India.
Book Free Demo Class →