What are the problems encountered in the example of the official python3.6 document scope and namespaces?

def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    do_local()
    print("After local assignment:", spam) -sharp test spam
    do_nonlocal()
    print("After nonlocal assignment:", spam) -sharp nonlocal spam
    do_global()
    print("After global assignment:", spam) -sharp nonlocal spam

scope_test()
print("In global scope:", spam)
  1. Why do_local () prints test spam instead of local spam
  2. When
  3. executes do_global () , since the variable spam is redeclared as a global variable in this function, why print nonlocal spam instead of test spam .
Mar.28,2021

first, print ("After local assignment:", spam) is always looking for the spam, of the current scope, that is,

def scope_test():
    ...
    spam = "test spam"  -sharp  spam :spam_4
    ...
    print("After local assignment:", spam) -sharp  spam_4 
    print("After nonlocal assignment:", spam) -sharp  spam_4 
    print("After global assignment:", spam) -sharp  spam_4 

answer the first question:
do_local () internal spam scope is only inside this do_local () , so no value is assigned to spam_4, so print test_spam .
answer the second question:
in executing do_nonlocal () , spam_4 has been assigned nonlocal spam . The assignment in
and do_global () does not change the value of spam_4, so print nonlocal spam instead.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35e56-2bfc1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35e56-2bfc1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?