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

Python Inner Classes

Inner class एक ऐसी class है जो किसी दूसरी class के अंदर रखी गई हो, जैसे एक बड़े cabinet के अंदर बनाया गया छोटा drawer। इसका इस्तेमाल तब होता है जब छोटा टुकड़ा सिर्फ बड़ी class के हिस्से के रूप में ही मायने रखता हो।
Syntax
python
class OuterClass:
    class InnerClass:
        # inner members

inner_object = OuterClass.InnerClass()

Inner Class क्या है

Inner class बस एक class है जो किसी दूसरी class की body के अंदर define की गई हो, जो इसके नाम को outer class के namespace के अंदर nest कर देती है — आप इसे एक free-standing top-level नाम की बजाय Outer.Inner के रूप में access करते हैं।

यह यह signal करने का एक तरीका है कि inner class सिर्फ अपनी outer class के context में ही मायने रखती है।

उदाहरण: What Is an Inner Class

python
class Outer:
    class Inner:  # class nested inside another class
        pass

print(Outer.Inner)  # accessed as Outer.Inner, not a top-level name

Inner Class क्यों इस्तेमाल करें

Inner classes तब उपयोगी होती हैं जब कोई helper class किसी एक specific outer class से tightly coupled हो और अकेले उसका कोई मतलब न हो — इसे nest करना यह relationship सीधे code structure में बताता है और module के top-level namespace को सिर्फ एक ही दूसरी class द्वारा इस्तेमाल किए जाने वाले नाम से भरने से बचाता है।

उदाहरण: Why Use an Inner Class

python
class Car:
    class Engine:  # tightly coupled helper, only meaningful within Car
        def start(self):
            print("Engine started")

Car.Engine().start()

Inner Class को Instantiate करना

आप किसी inner class का instance बिल्कुल किसी भी class की तरह बनाते हैं, बस उसके qualified path के through — Outer.Inner() — और inner class instance का किसी particular outer instance से कोई automatic special link नहीं होता जब तक आप explicitly एक पास न करें।

उदाहरण: Instantiating an Inner Class

python
class Outer:
    class Inner:
        def __init__(self):
            self.value = 42

obj = Outer.Inner()  # instantiated through its qualified path
print(obj.value)

Inner Classes बनाम अलग Top-Level Classes

नेस्टिंग को language द्वारा कहीं भी ज़रूरी नहीं ठहराया गया — आप हमेशा इसकी बजाय एक अलग top-level class लिख सकते थे, और ज़्यादातर Python style guides असल में एक छोटी tightly-scoped helper से आगे के लिए इसी को prefer करती हैं, क्योंकि nesting inner class को reuse, test, या दूसरे modules से reference करना मुश्किल बना देती है।

उदाहरण: Inner Classes vs Separate Top-Level Classes

python
class Engine:
    def start(self):
        print("Engine started")

class Car:
    def __init__(self):
        self.engine = Engine()  # composition using a separate top-level class instead of nesting

Car().engine.start()

Outer Class State को Access करना

Inner class के पास अपनी outer class के instance attributes तक वैसे automatic access नहीं होता जैसे किसी nested function closure के पास होता — अगर inner class को outer instance से data चाहिए, तो वह data explicitly पास करना होगा, आमतौर पर inner class के constructor के through।

उदाहरण: Accessing Outer Class State

python
class Car:
    class Engine:
        def __init__(self, owner_name):
            self.owner_name = owner_name  # data passed in explicitly, not inherited from Car

    def __init__(self, owner_name):
        self.engine = Car.Engine(owner_name)  # owner_name is forwarded to the inner class

car = Car("Alex")
print(car.engine.owner_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.