Advertisements
Advertisements
Question
Explain LEGB rule.
Explain
Advertisements
Solution
The LEGB rule specifies the order in which Python searches for a name:
- L - Local: Searches inside the current function.
- E - Enclosing: Searches in enclosing functions, from inner to outer.
- G - Global: Searches in the global namespace of the module.
- B - Built-in: Searches in Python's built-in namespace.
Thus, Python uses the first matching name found in the order: Local → Enclosing → Global → Built-in.
shaalaa.com
Is there an error in this question or solution?
