这个nonlocal是py3.x中才有的关键词
第一种情况,不使用nonlocal的情况:
#-*- encoding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') def test(): x=1 print("test="+str(x)) def test2(): #nonlocal x x=3 print("test2="+str(x)) test2()#这个的意思是在test()中测试使用test2()函数 print x if __name__ == '__main__': test()
运行结果:
第二种,使用nonlocal的情况
def test(): x=1 print("test:"+str(x)) def test2(): nonlocal x x=3 print("test2:"+str(x)) test2() print (x) if __name__ == '__main__': test()
运行结果:
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/228010.html原文链接:https://javaforall.net
