OOP in Python - Complete Guide in Hindi for AI Learners

Classes and objects usage

👨‍💻 OOP in Python - Object-Oriented Programming (हिंदी में)

Python में Classes, Objects और OOP Concepts को सीखें आसान उदाहरणों के साथ।

🔰 परिचय

OOP (Object-Oriented Programming) एक programming paradigm है जो Objects और Classes पर आधारित होता है। Python एक multi-paradigm भाषा है जिसमें OOP को बहुत अच्छे तरीके से implement किया गया है।

AI और ML models में अक्सर code को modular और reusable बनाने के लिए OOP concepts का प्रयोग होता है।

📦 Class और Object क्या होते हैं?

Class: एक blueprint या template है जिससे हम objects बनाते हैं।

Object: Class का एक instance होता है।

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def show(self):
        print("Name:", self.name, "Age:", self.age)

s1 = Student("Rahul", 20)
s1.show()

🔁 Constructor (__init__ Method)

Constructor वह method है जो object create करते समय अपने आप call होता है।

class Car:
    def __init__(self, brand):
        self.brand = brand

c1 = Car("Tata")
print(c1.brand)

🔐 Encapsulation (डेटा छुपाना)

Encapsulation में हम data को hide करते हैं और direct access को रोकते हैं।

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    def get_balance(self):
        return self.__balance

acc = BankAccount(10000)
print(acc.get_balance())

🧬 Inheritance (विरासत)

Inheritance से एक class दूसरी class की properties को प्राप्त कर सकती है।

class Animal:
    def speak(self):
        print("I am an animal")

class Dog(Animal):
    def bark(self):
        print("Woof")

d = Dog()
d.speak()
d.bark()

🎭 Polymorphism (बहुरूपता)

Polymorphism का मतलब होता है - एक चीज़ को कई रूप में उपयोग करना।

class Cat:
    def sound(self):
        print("Meow")

class Dog:
    def sound(self):
        print("Bark")

def animal_sound(animal):
    animal.sound()

animal_sound(Cat())
animal_sound(Dog())

📘 Practice और Projects

  • Student Management System using Classes
  • Bank Account Simulator using Encapsulation
  • Animal Simulator using Inheritance & Polymorphism

✅ निष्कर्ष

OOP in Python आपको structured और reusable code लिखने में मदद करता है। AI और बड़े प्रोजेक्ट्स में OOP का प्रयोग करना efficiency बढ़ाता है।

🚀 अगले ब्लॉग में: Python Libraries Overview (NumPy, Pandas, Matplotlib)