LEGB in Python

LEGB in Python

All names assigned inside a function def statement (or a lambda, an expression we’ll meet later) are locals by default. Functions can freely use names assigned in syntactically enclosing functions and the global scope, but they must declare such non locals and globals in order to change them.

फंक्शन def स्टेटमेंट (या बाद में मिलने वाला एक एक्सप्रेशन) के अंदर असाइन किए गए सभी नाम डिफॉल्ट रूप से लोकल हैं। फंक्शंस स्वतंत्र रूप से वाक्यविन्यास फंक्शंस और ग्लोबल स्कोप में निर्दिष्ट नामों का उपयोग कर सकते हैं, लेकिन उन्हें बदलने के लिए इस तरह के नॉनक्लॉक और ग्लोबल्स की घोषणा करनी चाहिए।

– Python’s name-resolution scheme is sometimes called the LEGB rule, after the scope names:

स्कोप नेम के बाद पायथन की नेम-रिजॉल्यूशन स्कीम को कभी-कभी LEGB नियम कहा जाता है।

  • When you use an unqualified name inside a function, Python searches up to four scopes the local (L) scope, then the local scopes of any enclosing (E) defs and lambdas, then the global (G) scope, and then the built-in (B) scope-and stops at the first place the name is found. If the name is not found during this search, Python reports an error.

जब आप किसी फंक्शन के अंदर एक अयोग्य नाम का उपयोग करते हैं, तो पायथन चार स्कोप तक खोजता है – लोकल (L) स्कोप, फिर किसी भी इनक्लोजिंग (E) के लोकल स्कोप और lambdas, फिर ग्लोबल (G) स्कोप, और फिर बिल्ट-इन (B) स्कोप – और पहली जगह पर स्टॉप मिलता है जिसका नाम पाया जाता है। यदि इस खोज के दौरान नाम नहीं मिला है, तो पायथन एक एरर की रिपोर्ट करता है।

  • When you assign a name in a function (instead of just referring to it in an expression), Python always creates or changes the name in the local scope, unless it’s declared to be global or nonlocal in that function.

जब आप किसी फंक्शन में एक नाम निर्दिष्ट करते हैं (केवल एक एक्सप्रेशन में इसे संदर्भित करने के बजाय), पायथन हमेशा लोकल स्कोप में नाम बनाता या बदलता है, जब तक कि वह उस फंक्शन में ग्लोबल या नॉन-लोकल नहीं होता है।

  • When you assign a name outside any function (i.e., at the top level of a module file, or at the interactive prompt), the local scope is the same as the global scope-the module’s namespace.

जब आप किसी फंक्शन (यानी किसी मॉड्यूल फाइल के शीर्ष स्तर पर, या इंटरेक्टिव प्रॉम्प्ट पर) के बाहर एक

नाम निर्दिष्ट करते हैं, तो लोकल स्कोप ग्लोबल स्कोप- मॉड्यूल का नेम स्पेस के समान होता है।

To wrap up the LEGB rule, let us come to the built-in scope. Here, we will define our “own” length function, which happens to bear the same name as the in-built len() function.

LEGB नियम को त्रैप के लिए, आइए हम बिल्ट-इन स्कोप में आते हैं। यहां, हम अपने “स्वयं” लेंथ-फंक्शन को परिभाषित करेंगे, जो कि इन-बिल्ट लेन) फंक्शन के समान नाम के लिए होता है।

What outcome do you expect if we’d execute the following code?
यदि हम निम्नलिखित कोड को निष्पादित नहीं करते हैं तो आप किस परिणाम की उम्मीद करते हैं?

a_var = 'global variable'
def len (in_var):
    print('called my len() function') 
    vlen = 0 
    for i in in_var:
        vlen += 1
    return vlen
def a_func(in_v):
    len_in_var = len (in_v)
    print ('Length of variable', len_in_var) 
a_func('Hello, This is Python!')

Output:
called my len() function
Length of variable 22

LEGB Rule: Variable used in inner function search for local scope, if not found, then it search for enclosed scope, if not found then it search for global scope, and if not found in global scope, then it search in built-in scope.

LEGB नियमः वैरिएबल का उपयोग लोकल स्कोप के लिए आंतरिक फंक्शन सर्च में किया जाता है, यदि नहीं मिला है, तो यह संलग्न क्षेत्र को सर्च करता है, यदि नहीं मिला है तो यह ग्लोबल स्कोप को सर्च करता है, और यदि ग्लोबल स्कोप में नहीं मिलता है, तो यह बिल्ट-इन स्कोप में सर्च करता है।
Example:

def outer_function () :
    a = 20 
    def inner_function ():
        a = 30 
        print ('a =',a)
    inner_function () 
    print ('a =',a)
a = 10
outer_function()
print('a =', a)

Output:
a = 30
a = 20
a = 10

Module Basics – मॉड्यूल बेसिक्स

We have seen that functions help us to reuse a particular piece of code. A module goes a step ahead. It allows you to reuse one more functions in your programs, even in the programs in which those functions have not been defined.

हमने देखा है कि फंक्शंस कोड के किसी विशेष भाग का पुनः उपयोग करने में हमारी सहायता करते हैं। एक मॉड्यूल एक कदम आगे जाता है। यह आपको अपने प्रोग्रामों में एक और फंक्शन का पुन: उपयोग करने की अनुमति देता है, यहां तक कि उन प्रोग्रामों में भी जिन्हें उन फंक्शंस को परिभाषित नहीं किया गया है।

A module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. The file name is the module name with the suffix .py appended.

एक मॉड्यूल पायथन कोड से युक्त एक फाइल है। एक मॉड्यूल फंक्शंस, क्लासेज और वैरियेबल को परिभाषित कर सकता है। एक मॉड्यूल रन करने योग्य कोड भी शामिल कर सकता है। फाइल नाम .py के साथ मॉड्यूल का नाम है।

We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide re-usability of code.

हम छोटे प्रबंधनीय और संगठित फाइलों में बड़े कार्यक्रमों को तोड़ने के लिए मॉड्यूल का उपयोग करते हैं। इसके अलावा, मॉड्यूल कोड की पुनः प्रयोज्य प्रदान करते हैं।

We can define our most used functions in a module and import it, instead of copying their definitions into different programs.

हम अपने सबसे अधिक उपयोग किए जाने वाले फंक्शंस को एक मॉड्यूल में परिभाषित कर सकते हैं और इसे इंपोर्ट कर सकते हैं, बजाय उनकी परिभाषा को अलग-अलग प्रोग्रामों में कॉपी कर सकते हैं।

Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable sub-tasks or modules. Individual modules can then be cobbled together like building blocks to create a larger application.

मॉड्यूलर प्रोग्रामिंग से तात्पर्य अलग, छोटे, अधिक प्रबंधनीय उप-प्रकारों या मॉड्यूलों में एक बड़े, अनजाने प्रोग्रामिंग कार्य को तोड़ने की प्रक्रिया से है। व्यक्तिगत मॉड्यूल को एक बड़ा अनुप्रयोग बनाने के लिए बिल्डिंग ब्लॉक्स की तरह एक साथ मोड़ा जा सकता है।

Advantages : लाभ

  • Re-usability: It allows the user to reuse the functionality with a different interface without typing the whole program again.

पुनः प्रयोज्यः यह यूजर को फिर से पूरे प्रोग्राम को टाइप किए बिना एक अलग इंटरफेस के साथ कार्यक्षमता का पुनः उपयोग करने की अनुमति देता है।

  • Ease of Use: This approach allows simplicity, as rather than focusing on the entire thousands and millions of lines code in one go we can access it in the form of modules. This allows ease in debugging the code and prone to less error.

उपयोग में आसानी: यह दृष्टिकोण सरलता की अनुमति देता है, एक बार में पूरे हजारों और लाखों लाइनों के कोड पर ध्यान देने के बजाय हम इसे मॉड्यूल के रूप में एक्सेस कर सकते हैं। इससे कोड को डीबग करने में आसानी होती है और त्रुटि कम होती है।

  • Ease of Maintenance: It helps in less collision at the time of working on modules, helping a team to work with proper collaboration while working on a large application.

रखरखाव में आसानी: यह मॉड्यूल पर काम करने के समय कम टकराव में मदद करता है, एक टीम को एक बड़े एप्लिकेशन पर काम करते समय उचित सहयोग के साथ काम करने में मदद करता है।

Chapter wise Model Paper Link

About Me