Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

क्लोज़र्स


एक बंद (Closure) एक ऐसा फंक्शन ऑब्जेक्ट है जो स्मृति में मौजूद न होने पर भी परिवेश के स्कोप्स के मूल्यों को याद रखता है। आइए इसे चरणबद्ध तरीके से समझें।

पहले, एक नेस्टेड फंक्शन एक ऐसा फंक्शन होता है जो किसी अन्य फंक्शन के भीतर परिभाषित होता है। यह ध्यान रखना बहुत महत्वपूर्ण है कि नेस्टेड फंक्शन परिवेशीय स्कोप के वेरिएबल्स को एक्सेस कर सकते हैं। हालांकि, कम से कम पायथन में, वे केवल पढ़ने लायक होते हैं। हालांकि, "nonlocal" कीवर्ड का उपयोग इन वेरिएबल्स के साथ विशेष रूप से करके उन्हें संशोधित किया जा सकता है।

उदाहरण के लिए:

def transmit_to_space(message):
    "यह परिवेशीय फंक्शन है"
    def data_transmitter():
        "यह नेस्टेड फंक्शन है"
        print(message)

    data_transmitter()

print(transmit_to_space("Test message"))

यह अच्छे से काम करता है क्योंकि 'data_transmitter' फंक्शन 'message' को एक्सेस कर सकता है। "nonlocal" कीवर्ड के उपयोग का प्रदर्शन करने के लिए, इसे देखें

def print_msg(number):
    def printer():
        "यहां हम nonlocal कीवर्ड का उपयोग कर रहे हैं"
        nonlocal number
        number=3
        print(number)
    printer()
    print(number)

print_msg(9)

बिना nonlocal कीवर्ड के, आउटपुट "3 9" होता, हालांकि इसके उपयोग के साथ, हमें "3 3" मिलता है, मतलब "number" वेरिएबल का मान संशोधित हो जाता है।

अब, हम नेस्टेड फंक्शन को कॉल करने की बजाए फंक्शन ऑब्जेक्ट को रिटर्न क्यों न करें। (याद रखें कि यहाँ फंक्शन भी ऑब्जेक्ट होते हैं। (यह पायथन है।))

def transmit_to_space(message):
    "यह परिवेशीय फंक्शन है"
    def data_transmitter():
        "यह नेस्टेड फंक्शन है"
        print(message)
    return data_transmitter

और हम फंक्शन को निम्नानुसार कॉल करें:

  def transmit_to_space(message):
    "यह परिवेशीय फंक्शन है"
    def data_transmitter():
        "यह नेस्टेड फंक्शन है"
        print(message)
    return data_transmitter

  fun2 = transmit_to_space("Burn the Sun!")
  fun2()

भले ही "transmit_to_space()" का निष्पादन समाप्त हो गया था, संदेश संरक्षित रहा। यह तकनीक जिससे डेटा को कुछ कोड के साथ संलग्न किया जाता है भले ही उन अन्य मूल फंक्शंस का अंत हो गया हो, पायथन में बंद (closures) कहलाती है।

लाभ: बंद (Closures) वैश्विक वेरिएबल्स के उपयोग से बच सकते हैं और डेटा छुपाने का कुछ रूप प्रदान करते हैं। (उदा. जब किसी क्लास में कुछ विधियों की आवश्यकता हो, तो बंद (closures) का उपयोग करें।)

साथ ही, फंक्शन को सजाने वाले (Decorators) पायथन में बंद (closures) का व्यापक रूप से उपयोग करते हैं।

अभ्यास

बंद (closures) का उपयोग करके कई गुणन योग्य फंक्शन प्राप्त करने के लिए एक नेस्टेड लूप और एक पायथन बंद (closure) बनाएं। यानी बंद (closures) का उपयोग करके कोई multiply_with_5() या multiply_with_4() फंक्शन बना सकता है।

# your code goes here multiplywith5 = multiplier_of(5) multiplywith5(9) def multiplier_of(n): def multiplier(number): return number*n return multiplier multiplywith5 = multiplier_of(5) print(multiplywith5(9)) test_output_contains("45") success_msg("Great work!")

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy