Python中 nonlocal与global的分析

    xiaoxiao2022-06-30  149

    nonlocal:声明该变量不是当前函数的本地变量 global:在函数内部声明变量为全局变量

    x=300 def mytest1(): x =200 def mytest2(): #global x nonlocal x print(x) x=100 print(x) return mytest2 t1 = mytest1() t1() #结果为: #200 #100 x=300 def mytest1(): x =200 def mytest2(): global x # nonlocal x print(x) x=100 print(x) return mytest2 t1 = mytest1() t1() #结果为: #300 #100

    最新回复(0)