Function Scope, Closures and Default Arguments Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Function Scope, Closures and Default Arguments notes for GATE DA: 18 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Function Scope, Closures and Default Arguments

    Chapter Roadmap

    Chapter Journey

    1
    Mutable Default Arguments (Current Topic)
    • Why default arguments are evaluated only once
    • The shared state trap with lists and dictionaries
    • The None pattern for safe defaults
    2
    Closures and Enclosed State
    • How inner functions capture variables from outer scopes
    • The nonlocal keyword and state retention
    • Factory functions and decorators

    Chapter Mastery

    • Predict the exact state of mutable default arguments across multiple function calls.
    • Distinguish between variable mutation and reassignment in default arguments.
    • Trace closure state and identify enclosed variables in nested functions.
    Weight hint: Both topics are highly conceptual. Expect code-tracing questions where a small misunderstanding of evaluation time or scope leads to the wrong output.

    Topic Hero: The Mutable Default Argument Trap

    Topic Hero: The Mutable Default Argument Trap

    The Core Intuition

    In Python, default arguments are evaluated exactly once — when the def statement is executed, not when the function is called.

    If the default value is a mutable object (like a list, dictionary, or set), that single object is shared across all function calls that use the default.

    Why this happens

    When Python reads a function definition, it evaluates the default expressions and stores them in the function object. Every time you call the function without providing that argument, Python passes the exact same object reference.

    Argument Type Evaluated When Behavior
    Immutable (int, str, tuple) Definition time Safe. Reassignment creates a new local object.
    Mutable (list, dict, set) Definition time Trap. Mutations affect the shared default object.

    Definition Time vs Call Time Evaluation

    Definition Time vs Call Time Evaluation

    1. Definition Time

    The def keyword is executed. Python creates the function object and evaluates the default arguments.

    2. Call Time

    The function is invoked. Python binds the arguments to the parameter names and executes the body.

    Inspecting the Defaults

    You can actually see the stored default objects using the __defaults__ attribute:

    def my_func(a, b=[]):
        pass
    print(my_func.__defaults__)

    This prints ([],). The list is created once and stored inside the function object. Every call that uses the default b gets a reference to this exact same list.

    Tracing the Classic append_to_lst Example

    Tracing the Classic append_to_lst Example

    The Code

    def append_to_lst(val, lst=[]):
        lst.append(val)
        return lst
    
    print(append_to_lst(1))
    print(append_to_lst(2))
    print(append_to_lst(3, []))
    Default List Object
    Explicit List Object (Call 3)
    Current Call: 0

    Step-by-Step Trace

    • Call 1: append_to_lst(1)
      val is 1. lst binds to default []. Mutates default to [1]. Returns [1].
    • Call 2: append_to_lst(2)
      val is 2. lst binds to default (now [1]). Mutates to [1, 2]. Returns [1, 2].
    • Call 3: append_to_lst(3, [])
      val is 3. lst is explicitly provided as new []. Mutates explicit list to [3]. Returns [3]. Default remains [1, 2].

    Final Output

    [1]
    [1, 2]
    [3]

    More notes in this unit

    chapter
    Function Scope, Closures and Default Arguments Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Function Scope, Closures and Default Arguments notes for GATE DA: 18 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice q

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.