語法 :: nonlocal 陳述句

  • 在區域函式中使用
def f1():
    x = 1

    def f2():
        x = 2

    print(x) # 1
def f1():
    x = 1

    def f2():
        nonlocal x
        x = 2

    print(x) # 2
def f1():
    x = 1

    def f2():
        nonlocal x = 2 # SyntaxError: invalid syntax
        x = 2

    print(x) # 2
x = 1

def f():
    nonlocal x
    x = 2

# SyntaxError: no binding for nonlocal 'x' found

results matching ""

    No results matching ""