nonlocalAdvanced Playground

Declares a variable inside a nested function as belonging to the enclosing scope

Python Playground
Output
Click "Run" to execute your code

Decorators naturally create closures. nonlocal is useful when a decorator needs to maintain state between calls (like counting retries or caching results).

Challenge

Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?