Q10: What is the LEGB Rule for Python scope resolution?

Python resolves variable names in strict sequential order:

  1. L (Local): Inside active function/lambda body.
  2. E (Enclosing / Nonlocal): In enclosing nested function scopes.
  3. G (Global): At module top-level.
  4. B (Built-in): Preloaded Python built-in names (len, range).
Key Takeaways
  • Scope resolution order: Local -> Enclosing -> Global -> Built-in.
  • Use global or nonlocal keywords to rebind variables in outer scopes.
Knowledge Check

Which keyword must be used inside a nested inner function to rebind a variable declared in its parent outer function scope?