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%
Build a utility that demonstrates Python's exception handling behaviors, specifically exception variable scope and try-finally-return interactions.
@generates
Create a Python module that implements two functions to analyze exception handling behavior:
Implement a function that demonstrates Python's automatic exception variable deletion after except blocks.
Implement a function that shows how finally blocks can override return values.
def analyze_exception_scope():
"""
Analyzes Python's exception variable deletion behavior.
Catches an exception in an except block, then attempts to access
the exception variable outside the except block.
Returns:
dict: A dictionary containing:
- 'caught': bool - whether an exception was caught
- 'accessible': bool - whether the variable is accessible outside except
- 'error_type': str or None - name of error raised when accessing variable
"""
pass
def test_finally_return(trigger_exception: bool):
"""
Demonstrates how finally block return values override try/except returns.
Args:
trigger_exception: If True, raises an exception to test except path;
if False, tests the normal try path
Returns:
str: The actual return value (always 'finally' due to override behavior)
"""
passProvides educational examples of Python's surprising behaviors.
The module should clearly demonstrate Python's automatic exception variable cleanup (to prevent reference cycles) and the finally block's ability to override return values from try or except blocks. These behaviors are often surprising to developers unfamiliar with Python's exception handling internals.