Testing global vars scope,I found with this situation, and can't understand why first function works and sencond don't:
- the error UnboundLocalError
is raised at same line -
_D = {'a': 1}
def read():
print(_D['a'])
def show_and_update():
print(_D['a'])
_D = {'a': 1}
read()
> 1
show_and_update()
Traceback (most recent call last):
File "/home/anybody/.local/share/virtualenvs/nsite-scoli-meter-cx__qsm9/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-58-a57000dda288>", line 1, in <module>
show_and_update()
File "<ipython-input-56-875e227c8e57>", line 2, in show_and_update
print(_D['a'])
UnboundLocalError: local variable '_D' referenced before assignment