Home › Hindi › OOPs concepts Hindi
Python OOPs concepts Hindi में — class, object, self और inheritance
🌐 यह page Hindi में है · Read this in English →
चारों pillars examples के साथself का असली मतलबFree browser practice
Class और object — एक मिसाल से
Class एक नक्शा है, object उस नक्शे से बना असली घर। नक्शे में कोई रह नहीं सकता, घर में रह सकते हैं।
class Student:
def __init__(self, naam, marks):
self.naam = naam
self.marks = marks
def result(self):
return "Pass" if self.marks >= 33 else "Fail"
# objects बनाए
riya = Student("Riya", 85)
aman = Student("Aman", 20)
print(riya.naam, riya.result()) # Riya Pass
print(aman.naam, aman.result()) # Aman Fail
एक class से जितने चाहो objects बनाओ — हर object का अपना अलग data होता है, पर methods सबके लिए एक ही हैं।
__init__ और self — दो सबसे बड़े confusions
__init__ constructor है — object बनते ही अपने आप चलता है। आप इसे खुद कभी call नहीं करते; Student("Riya", 85) लिखते ही Python खुद चला देता है।
self का मतलब है "यही वाला object"। जब आप riya.result() लिखते हो, Python उसे अंदर-ही-अंदर Student.result(riya) बना देता है — इसलिए हर method का पहला parameter self होना ज़रूरी है।
# self भूल गए तो
class Demo:
def show():
print("hello")
Demo().show()
TypeError: show() takes 0 positional arguments but 1 was given
यह error message उल्टा लगता है — "0 चाहिए थे पर 1 मिला" — क्योंकि Python ने चुपचाप object खुद भेज दिया था। def show(self): लिखते ही ठीक हो जाएगा।
Instance variable बनाम class variable
class Student:
school = "PyDebug School" # class variable — सबके लिए एक
def __init__(self, naam):
self.naam = naam # instance variable — हर object का अलग
a = Student("Riya")
b = Student("Aman")
print(a.school, b.school) # PyDebug School PyDebug School
Student.school = "New School"
print(a.school, b.school) # New School New School ← दोनों बदल गए
असली trap: class variable अगर list हो तो सारे objects एक ही list share करेंगे — यह interview का favourite सवाल है:
class Box:
items = [] # ❌ सबके लिए एक ही list
def add(self, x):
self.items.append(x)
p, q = Box(), Box()
p.add(1)
print(q.items) # [1] 😱
# ✅ Fix: हर object की अपनी list __init__ में बनाओ
class Box:
def __init__(self):
self.items = []
Inheritance — पुरानी class से नई बनाना
class Person:
def __init__(self, naam):
self.naam = naam
def intro(self):
return f"Main {self.naam} hoon"
class Student(Person): # Person की सब चीज़ें मिल गईं
def __init__(self, naam, roll):
super().__init__(naam) # parent का __init__ चलाओ
self.roll = roll
def intro(self): # method overriding
return f"{super().intro()}, roll {self.roll}"
print(Student("Riya", 12).intro()) # Main Riya hoon, roll 12
super() parent class को बुलाता है। __init__ में super().__init__() लिखना मत भूलो, वरना parent के variables बनेंगे ही नहीं और बाद में AttributeError आएगा।
Encapsulation और Polymorphism
Encapsulation = data को बाहर से सीधे छेड़ने से बचाना। Python में underscore इसका इशारा है:
class Account:
def __init__(self, balance):
self.__balance = balance # double underscore = private जैसा
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def get_balance(self):
return self.__balance
a = Account(100)
a.deposit(50)
print(a.get_balance()) # 150
# print(a.__balance) # ❌ AttributeError
Polymorphism = एक ही नाम, अलग-अलग behaviour:
class Dog:
def bolo(self): return "Bhow"
class Cat:
def bolo(self): return "Meow"
for jaanwar in [Dog(), Cat()]:
print(jaanwar.bolo()) # Bhow / Meow
Loop को फर्क नहीं पड़ता कि object किस class का है — बस bolo() होना चाहिए। Python में इसी को "duck typing" कहते हैं।
याद रखने लायक 5 बातें
- हर method का पहला parameter
self— भूले तोTypeError। __init__खुद चलता है, आप call नहीं करते।- Mutable (list/dict) default कभी class level पर मत रखो —
__init__में बनाओ। - Child class में
super().__init__()लिखना मत भूलो। print(object)अजीब दिखे तो class में__str__method जोड़ो।
OOP असल में एक ही सवाल का जवाब है: "जो data और जो काम साथ-साथ चलते हैं, उन्हें एक जगह कैसे रखें?" — class यही करती है।
🛠️ Practice करो (free, browser में)
पढ़ना काफी नहीं — यही bug खुद fix करके देखो। नीचे के problems browser में ही चलते हैं, कोई install नहीं, कोई login नहीं।
- Class variable सब objects में share — interview का favourite
- Function जो कुछ return नहीं करता — None कहाँ से आया
- Default parameter values — __init__ में भी यही rule
- Mutable default argument trap — वही list वाली galti
अक्सर पूछे जाने वाले सवाल
Python में self का क्या मतलब है?
self उसी object की तरफ इशारा करता है जिस पर method चलाया जा रहा है। riya.result() लिखने पर Python अंदर ही अंदर Student.result(riya) बनाता है, इसलिए पहला parameter self ज़रूरी है।
__init__ को constructor क्यों कहते हैं?
क्योंकि object बनते ही यह अपने आप सबसे पहले चलता है और object के शुरुआती variables सेट करता है। आपको इसे खुद call नहीं करना पड़ता।
class variable और instance variable में क्या फर्क है?
Class variable class के अंदर, किसी method के बाहर बनता है और सारे objects उसे share करते हैं। Instance variable self. के साथ __init__ में बनता है और हर object का अपना अलग होता है।
super() का इस्तेमाल क्यों करते हैं?
super() parent class का method बुलाने के लिए है। खास तौर पर child के __init__ में super().__init__() लिखना ज़रूरी है, वरना parent के variables बनेंगे ही नहीं और बाद में AttributeError आएगा।