To understand nonlocal and global in Python 3, I read Python nonlocal statement
Also, I read, https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces where in I modified the example a bit with this code:
def scope_test():
def do_global():
global spam
print(f'spam: {spam}')
spam = "global spam"
spam = "test spam"
do_global()
print("After global assignment:", spam)
scope_test()
print("In global scope:", spam)
# spam: global spam
# After global assignment: test spam
# In global scope: global spam
I am not sure, why the first print statement returns 'global spam', but I am also not sure, what ideally it should have returned.
Please help me understand how before assignment of name as 'global spam' print statement was able to write it.
Thanks in advance.