Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals
Overall
score
93%
A Python utility that demonstrates variable scope behavior and closure handling.
@generates
def create_closure_functions(n: int) -> list:
"""
Creates a list of n functions where each function returns its index.
Must properly handle closure variable capture.
Args:
n: Number of functions to create
Returns:
List of functions, where the i-th function returns i
"""
pass
def create_lambda_closures(values: list) -> list:
"""
Creates a list of lambda functions that capture values from the input list.
Args:
values: List of values to capture
Returns:
List of lambda functions, where the i-th lambda returns values[i]
"""
pass
def modify_with_nonlocal(initial: int, increment: int) -> int:
"""
Uses an inner function to modify an enclosing scope variable.
Args:
initial: Initial value of the enclosing variable
increment: Amount to increment by
Returns:
The modified value after incrementing
"""
pass
def set_global_counter(value: int) -> None:
"""
Sets the module-level variable 'counter' to the given value.
Args:
value: Value to set for the global counter
"""
pass
def trigger_unbound_local_error() -> None:
"""
Demonstrates UnboundLocalError by reading before assignment.
Raises:
UnboundLocalError: Always raised when called
"""
passUses built-in Python features for scope and closure demonstration. No external dependencies required.
Install with Tessl CLI
npx tessl i tessl/pypi-wtfpythondocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10