tessl install tessl/pypi-wtfpython@3.0.0Educational 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%
A Python utility that analyzes closures and demonstrates proper handling of variable binding in nested functions and generators.
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.
x with value 10, the inspector returns a dictionary containing the variable name and its current value @testBuild a function that creates a list of closures where each closure captures loop variables, demonstrating late binding behavior.
i, all closures return the final loop value when called (demonstrating late binding) @testBuild 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.
Build a function that creates a generator with closure variables and returns information about the generator's captured scope.
@generates
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
"""
passProvides introspection capabilities for examining closures and generator functions.
@satisfied-by