or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/wtfpython@3.0.x
tile.json

tessl/pypi-wtfpython

tessl install tessl/pypi-wtfpython@3.0.0

Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.06x

Baseline

Agent success rate without this tile

88%

task.mdevals/scenario-9/

Closure Variable Inspector

A Python utility that analyzes closures and demonstrates proper handling of variable binding in nested functions and generators.

Capabilities

Inspect closure variables

Build a function that takes a closure (a nested function that captures variables from an outer scope) and returns detailed information about the captured variables.

  • Given a closure that captures a single variable x with value 10, the inspector returns a dictionary containing the variable name and its current value @test
  • Given a closure that captures multiple variables from different scopes, the inspector returns all captured variables with their current values @test
  • Given a nested closure (a closure within a closure), the inspector correctly identifies all captured variables from all enclosing scopes @test

Create late-binding closures

Build a function that creates a list of closures where each closure captures loop variables, demonstrating late binding behavior.

  • Creating 3 closures in a loop where each captures the loop index i, all closures return the final loop value when called (demonstrating late binding) @test
  • Given closures created with a mutable captured variable, modifying the variable affects all closures that captured it @test

Create early-binding closures

Build a function that creates a list of closures with early binding, where each closure captures the loop variable's value at the time of creation.

  • Creating 3 closures in a loop with early binding, each closure returns its own captured value (0, 1, 2) when called @test
  • The early-bound closures remain independent even when the original loop variable changes @test

Analyze generator scoping

Build a function that creates a generator with closure variables and returns information about the generator's captured scope.

  • Given a generator that captures a variable from the outer scope, the analyzer identifies the captured variable and its value @test
  • Given a generator with multiple yield points that reference different captured variables, the analyzer lists all captured variables @test

Implementation

@generates

API

from typing import Callable, Dict, Any, List, Generator

def inspect_closure(func: Callable) -> Dict[str, Any]:
    """
    Inspects a closure and returns information about captured variables.

    Args:
        func: A closure function to inspect

    Returns:
        A dictionary mapping variable names to their current values

    Raises:
        ValueError: If the provided function is not a closure
    """
    pass

def create_late_binding_closures(n: int) -> List[Callable[[], int]]:
    """
    Creates a list of n closures that demonstrate late binding behavior.
    Each closure captures the loop variable, which will have the final value
    when any closure is called.

    Args:
        n: Number of closures to create

    Returns:
        A list of closures that all return the final loop value
    """
    pass

def create_early_binding_closures(n: int) -> List[Callable[[], int]]:
    """
    Creates a list of n closures that demonstrate early binding behavior.
    Each closure captures its own copy of the loop variable at creation time.

    Args:
        n: Number of closures to create

    Returns:
        A list of closures where closure i returns i
    """
    pass

def analyze_generator_scope(gen_func: Callable[[], Generator]) -> Dict[str, Any]:
    """
    Analyzes the closure variables captured by a generator function.

    Args:
        gen_func: A generator function to analyze

    Returns:
        A dictionary mapping captured variable names to their values

    Raises:
        ValueError: If the provided function is not a generator function
    """
    pass

Dependencies { .dependencies }

inspect { .dependency }

Provides introspection capabilities for examining closures and generator functions.

@satisfied-by