Quick actions

cmd+k|ctrl+k

Navigation

Languages

nonlocal2

Snippet info

Language

Python

Visibility

public

Author

fai

Created

2024-10-05T03:07:09.322544Z

Updated

2024-10-05T03:07:09.322544Z

x = 100
def outer():
    global x
    x = 'hello'
    def inner1():
        #nonlocal x; with this line, error arises. Cause it conflicts with global in the top layer of function
        x = 'python'
        def inner2():
            nonlocal x
            x = 'monty'
        print('inner1 (before):', x)
        inner2()
        print('inner1 (after):', x)
    inner1()
    print('outer:', x)
outer()
# line sequence: 16, 2, 3, 4 14, 5, 7, 11, 12, 8, 9, 10, 13, 15
#inside inner2, x change from python to monty,
#insdie inner1, x is python. 
#insdie outer, x is hello as global declared
INFO