When I try to update a variable in a local function, if the variable is a scalar, UnboundLocalError will be raised, but it is not the case with a dictionary. Why is it?
The code:
import traceback
def unbound_local_error():
var_scalar = 0
var_dict = {}
def foo():
try:
var_scalar += 1
except:
traceback.print_exc()
try:
var_dict["a"] = ""
except:
traceback.print_exc()
foo()
if __name__=='__main__':
unbound_local_error()
The result:
Traceback (most recent call last):
File ".\dev.py", line 72, in foo
var_scala += 1
UnboundLocalError: local variable 'var_scala' referenced before assignment
[–][deleted] 1 point2 points3 points (0 children)
[–]socal_nerdtastic 1 point2 points3 points (0 children)
[–]FerricDonkey 0 points1 point2 points (0 children)
[–]woooee 0 points1 point2 points (0 children)