Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals
Overall
score
93%
{
"context": "This evaluation assesses how well the engineer understands and applies Python's scope and name resolution rules (LEGB), particularly closure variable capture, the nonlocal and global keywords, and UnboundLocalError behavior.",
"type": "weighted_checklist",
"checklist": [
{
"name": "Closure capture technique",
"description": "In create_closure_functions(), properly captures loop variables using either default arguments (e.g., lambda i=i: i) or by creating a factory function that returns a closure. Avoids the late binding pitfall where all functions would return the same value.",
"max_score": 25
},
{
"name": "Lambda closure capture",
"description": "In create_lambda_closures(), correctly captures values from the list using lambda with proper binding (e.g., lambda v=val: v or similar technique) to ensure each lambda returns the corresponding value from the original list.",
"max_score": 20
},
{
"name": "Nonlocal keyword usage",
"description": "In modify_with_nonlocal(), correctly uses the 'nonlocal' keyword within a nested function to modify a variable from the enclosing scope. The inner function successfully modifies the enclosing variable and the modified value is returned.",
"max_score": 20
},
{
"name": "Global keyword usage",
"description": "In set_global_counter(), correctly uses the 'global' keyword to modify the module-level 'counter' variable. Declares 'global counter' before assigning to it.",
"max_score": 20
},
{
"name": "UnboundLocalError demonstration",
"description": "In trigger_unbound_local_error(), correctly demonstrates UnboundLocalError by attempting to read or use a variable (e.g., with print() or in an expression) before assigning to it in the same local scope. This must actually trigger the error, not just raise it manually.",
"max_score": 15
}
]
}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