Skip to main content

Explain Python variable scoping rules.

Entry Python
Quick Answer Python scoping follows LEGB rule: Local (inside function), Enclosing (in outer function for nested functions), Global (module level), Built-in (Python built-ins). Variables are looked up in this order. global keyword declares a variable is from global scope. nonlocal keyword (Python 3) modifies variable from enclosing function scope.

Answer

Scopes:
• Local
• Enclosing
• Global
• Built-in
Use global and nonlocal to modify outer variables.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice