English

Explain LEGB rule.

Advertisements
Advertisements

Question

Explain LEGB rule.

Explain
Advertisements

Solution

The LEGB rule specifies the order in which Python searches for a name:

  1. L - Local: Searches inside the current function.
  2. E - Enclosing: Searches in enclosing functions, from inner to outer.
  3. G - Global: Searches in the global namespace of the module.
  4. 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?
Chapter 3: Classes in Python - EXERCISE [Page 62]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 3 Classes in Python
EXERCISE | Q 4. | Page 62
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×