← Back to Python Course | Chapter 7: OOP | Lesson 3 of 11

Python Constructors

Constructor वह setup step है जो किसी नए object के बनते ही चलता है, जैसे किसी नए student के आने पर name tag भरना। यह object को उसकी starting values देता है।
Syntax
python
class ClassName:
    def __init__(self, parameter):
        self.attribute = parameter

object_name = ClassName(argument)

Constructor क्या है?

Constructor वह method है जिसे Python object बनते ही अपने-आप call करता है, और Python में वह __init__ है — नाम के बावजूद, यह object allocate नहीं करता (वह पर्दे के पीछे __new__ करता है), यह बस पहले से मौजूद object के attributes को initialize करता है।

उदाहरण: What is a Constructor?

python
class Dog:
    def __init__(self):  # __init__ runs automatically when a Dog is created
        print("A new dog is created")

Dog()

Default Constructor

एक constructor जो सिर्फ self लेता है और कोई और parameter नहीं, हर नए object को बिल्कुल वही starting values देता है, जो simple cases के लिए ठीक है पर इसका मतलब है कि आप creation के समय object को customize नहीं कर सकते — उदाहरण के लिए हर Dog() एक जैसे नाम से शुरू होता।

उदाहरण: Default Constructor

python
class Dog:
    def __init__(self):
        self.name = "Unnamed"  # every Dog starts with the same fixed name

d = Dog()
print(d.name)

Parameterized Constructor

self के अलावा extra parameters accept करना (def __init__(self, name, breed):) class के हर call को अपनी values देने देता है, इसलिए Dog(Rex, Labrador) और Dog(Milo, Poodle) एक ही blueprint से genuinely अलग objects बन जाते हैं।

उदाहरण: Parameterized Constructor

python
class Dog:
    def __init__(self, name, breed):  # extra parameters let each call customize the object
        self.name = name
        self.breed = breed

rex = Dog("Rex", "Labrador")
print(rex.name, rex.breed)

Default Parameter Values

किसी constructor parameter को default value देना (def __init__(self, name=Unnamed):) उस argument को optional बना देता है — जो callers नाम नहीं पास करते उन्हें fallback मिलता है, जो पास करते हैं वे उसे override कर देते हैं, बिना दो अलग constructors की ज़रूरत के।

उदाहरण: Default Parameter Values

python
class Dog:
    def __init__(self, name="Unnamed"):  # default makes the name argument optional
        self.name = name

print(Dog().name)  # uses the default
print(Dog("Rex").name)  # overrides the default

self की भूमिका

self उस specific object को दर्शाता है जो अभी बन रहा है या जिस पर काम हो रहा है, इसीलिए __init__ के अंदर 'self.name = name' पास की गई value को किसी shared या global variable में नहीं, उसी particular instance पर store करता है — हर method को यह जानने के लिए self चाहिए कि वह किस object पर काम कर रहा है।

उदाहरण: The Role of self

python
class Dog:
    def __init__(self, name):
        self.name = name  # self refers to this specific Dog being created

d = Dog("Rex")
print(d.name)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.