๐ฅUnderstanding OOP in Python
Today I explored Object-Oriented Programming (OOP) in Python โ a very important concept in software development and data analytics.
OOP helps us write clean, reusable, and scalable code, especially when building data pipelines and machine learning projects.
๐ง What is OOP?
OOP is a programming approach where we organize code using Classes and Objects.
โ Class
A class is a blueprint/template for objects.
โ Object
An object is an instance of a class.
๐ Example
class Student:
def __init__(self, name):
self.name = name
s1 = Student("Ramya")
print(s1.name)
๐๏ธ Four Pillars of OOP With Examples
1๏ธโฃ Encapsulation
Encapsulation means wrapping data (variables) and methods together, and controlling access.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private variable
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
acc = BankAccount(5000)
acc.deposit(2000)
print(acc.get_balance())
Why useful: Hides sensitive data like user credentials or balance.
2๏ธโฃ Inheritance
Inheritance allows one class to acquire properties of another class.
class Animal:
def sound(self):
print("Animals make sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
d = Dog()
d.sound()
Why useful: Helps reuse code โ no repeating functions.
3๏ธโฃ Polymorphism
Polymorphism means same function name, different behavior.
class Shape:
def area(self):
pass
class Square(Shape):
def area(self):
return "Area = side * side"
class Circle(Shape):
def area(self):
return "Area = ฯ * r * r"
print(Square().area())
print(Circle().area())
Why useful: Makes code flexible โ same function works differently depending on object.
4๏ธโฃ Abstraction
Abstraction means hiding implementation details and showing only necessary information.
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vehicle):
def start(self):
print("Car engine starts")
c = Car()
c.start()
Why useful: Simplifies complex code, improves security.
๐ก Why OOP is useful in Data Analytics?
โ
Reusable data preprocessing functions
โ
Cleaner code for ML pipelines
โ
Object models for datasets, features, models
โ
Better collaboration & scalability
๐ฃ My Progress Continues
Today was all about foundations, and these concepts are key for becoming a professional Data Analyst and understanding machine learning frameworks like Scikit-Learn, PyTorch, etc.
More learning every dayโฆ one step at a time ๐ช
๐ท๏ธ Tags
#python #beginners #dataanalytics #learning #oop #devto
Top comments (0)