Python Tutorial 0/52 lessons ~6 min read Lesson 13

    Scope & Closures (LEGB)

    Python resolves names using LEGB: Local → Enclosing → Global → Built-in.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Python resolves names using LEGB: Local → Enclosing → Global → Built-in. Closures capture enclosing-scope variables — essential for decorators.

    Understanding the topic

    Core concepts to understand:

    • Local: inside the function.
    • Enclosing: outer function (closure).
    • Global: module level.
    • Built-in: print, len, etc.
    • nonlocal and global declare write-through.

    Syntax reference

    Visual flow / code:

    python
    def make_counter():
    count = 0
    def increment():
    nonlocal count
    count += 1
    return count
    return increment
    c = make_counter()
    print(c()) # 1
    print(c()) # 2
    print(c()) # 3

    Execution workflow

    1Scope & Closures (LEGB) Workflow
    1 / 4

    Local

    inside the function.

    Real-world use

    Closures power decorators, factory functions, and stateful callbacks. Understand LEGB before tackling decorators.

    Best practices

    • Avoid global — return values instead.
    • Use nonlocal for closures.
    • Keep state in classes when complex.

    Common mistakes

    • Forgetting nonlocal creates a new local var, not the outer one.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat's LEGB?+

    Answer

    LEGB is the order in which Python looks up a name: Local (the current function), Enclosing (outer functions), Global (the module), and Built-in (the builtins module). Python uses the first match it finds. Assigning to a name inside a function makes it local to that function for the whole function body unless declared otherwise.

    Follow-up

    What error occurs if you read a local variable before assigning it?
    2IntermediateQuestionWhat's a closure?+

    Answer

    A closure is a function that keeps references to variables from its enclosing scope even after that scope has finished executing. Those captured variables are stored in cells, available via __closure__. Closures are used for function factories and decorators, but note they capture variables, not values, which causes the classic late-binding issue in loops.

    Follow-up

    Why do lambdas created in a loop often all see the last loop value?
    3IntermediateQuestion<code>nonlocal</code> vs <code>global</code>?+

    Answer

    global tells a function that an assigned name refers to the module-level variable, while nonlocal refers to a variable in the nearest enclosing function scope (not the module). nonlocal requires the name to already exist in an enclosing function and is used for mutable state in closures. You only need either keyword to rebind a name; mutating an object, such as appending to a list, does not need them.

    Summary

    In summary: LEGB rules name lookup. Closures = functions + captured scope.

    Ready to mark this lesson complete?Track your journey across the entire course.