Puzzle 16: Explanation
Let’s look at the scope of variables in Python and the cause of the `UnboundLocalError` exception.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to verify the result:
Explanation
When we have a variable, or name, in Python like cart = [‘lamp’]), we can do two operations:
- Mutate the variable by changing the object that the variable is pointing to (
cart.append(‘mug’)). - Rebind the variable by having the variable point to another