[Solved-4 Solutions] Python variable scope error
Error Description:
- The following code works as expected in both Python 2.5 and 3.0:
- However, when we uncomment line (B), we get an UnboundLocalError : 'c' not assigned at line (A). The values of a and b are printed correctly. This can raise up two questions:
- Why is there a runtime error thrown at line (A) because of a later statement on line (B)?
- Why are variables a and b printed as expected, while c raises an error?
Solution 1:
- as the first line of the function.
- As for python 3, there is now
- That you can use to refer to the nearest enclosing function scope that has a c variable.
Solution 2:
- Also, the reason you are getting this error is because you can also declare a new variable inside that function with the same name as a 'global' one, and it would be completely separate.
- The interpreter thinks you are trying to make a new variable in this scope called c and modify it all in one operation, which isn't allowed in Python because this new c wasn't initialized.
Solution 3:
Solution 4:
- This is not a direct answer to your question, but it is closely related, as it's another gotcha caused by the relationship between augmented assignment and function scopes.
- In most cases, you tend to think of augmented assignment (a += b) as exactly equivalent to simple assignment (a = a + b). It is possible to get into some trouble with this though, in one corner case. Let me explain:
- The way Python's simple assignment works means that if a is passed into a function (like func(a) ; note that Python is always pass-by-reference), then a = a + b will not modify the athat is passed in. Instead, it will just modify the local pointer to a.
- But if you use a += b, then it is sometimes implemented as:
- In the first case (as long as a is not declared global), there are no side-effects outside local scope, as the assignment to a is just a pointer update.
- In the second case, a will actually modify itself, so all references to a will point to the modified version. This is demonstrated by the following code:
- So the trick is to avoid augmented assignment on function arguments (I try to only use it for local/loop variables). Use simple assignment, and you will be safe from ambiguous behaviour.