<jupyter_start><jupyter_text>Example of static scoping and reference environments in Python 3.Code adapted from the Sebesta textbook Concepts of Programming Languages.<jupyter_code>g = 3
def sub1():
    a = 5
    b = 7
    print("sub1 g:", g)
    print("sub1 a:", a)
    print("sub1 b:", b)
    def sub2():
        # global g
        c = 9
        print("sub2 g:", g)
        print("sub2 c:", c)
        def sub3():
                # nonlocal c
                g=11
                print("sub3 g:", g)
                print("sub3 c:", c)
        sub3()        
    sub2()
sub1()
print("after:", g)             # global value of g 
# Looking at the role of global
g = 3
def sub1():
    g = 1                        # added this; changes local only
    a = 5
    b = 7
    print("sub1 g:", g)
    print("sub1 a:", a)
    print("sub1 b:", b)
    def sub2():
        global g
        c = 9
        g = 5                     # added this; here changes global
        print("sub2 g:", g)
        print("sub2 c:", c)
        def sub3():
                # nonlocal c
                g=11
                print("sub3 g:", g)
                print("sub3 c:", c)
        sub3()        
    sub2()
sub1()
print("after:", g)             # global value of g 
# Looking at the role of nonlocal
g = 3
def sub1():
    a = 5
    b = 7
    print("sub1 g:", g)
    print("sub1 a:", a)
    print("sub1 b:", b)
    def sub2():
        # global g
        c = 9
        g = 5            
        print("sub2 g:", g)
        print("sub2 c:", c)
        def sub3():
                #  nonlocal c         # let's toggle this one; can read value of parent in any case...
                g=11
                print("sub3 g:", g)
                print("sub3 c:", c)
        sub3()     
    sub2()
sub1()
print("after:", g)             
# Looking at the role of nonlocal
g = 3
def sub1():
    a = 5
    b = 7
    print("sub1 g:", g)
    print("sub1 a:", a)
    print("sub1 b:", b)
    def sub2():
        global g         
        c = 9
        g = 5            
        print("sub2 g:", g)
        print("sub2 c:", c)
        def sub3():
                nonlocal c  # here we've declared c as non-local, can read and write 
                c=6         # here we assign new value to c defined in sub2
                g=11
                print("sub3 g:", g)
                print("sub3 c:", c)  
        sub3() 
        print("sub2 after c:", c)    # so value of c changes also in sub2 due to the nonlocal
    sub2()
sub1()
print("after:", g)             
# Looking at the role of nonlocal
g = 3
def sub1():
    a = 5
    b = 7
    print("sub1 g:", g)
    print("sub1 a:", a)
    print("sub1 b:", b)
    def sub2():
        global g         
        c = 9
        g = 5            
        print("sub2 g:", g)
        print("sub2 c:", c)
        def sub3():
                # nonlocal c  # let's comment this out again
                c=6         # here we assign new value to c defined in sub2, but it is only local
                g=11
                print("sub3 g:", g)
                print("sub3 c:", c)  
        sub3() 
        print("sub2 after c:", c)  # the local assignment did not change c of sub2 
    sub2()
sub1()
print("after:", g)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12442254388666461, 'fraction_numerical': 0.033569448721897134, 'mean_word_length': 1.2825017568517216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '449383', 'license': 'MIT', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1006, 'n_words': 378, 'path': 'Nonlocal_global.ipynb', 'repo_name': 'schwartz-cnl/Programming_Languages', 'size': '3247'}