今天就跟大家聊聊有关python中global和nonlocal关键字有什么区别,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
global关键字:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information.>>> info = '全局'>>> def fo(): info = '外层函数'def fi():global info info = info + '1234'print(info) fi()print(info)>>> info'全局'>>> fo()全局1234外层函数>>> info'全局1234'>>> >>>
演示nonlocal关键字:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information.>>> info = '全局'>>> def fo(): info = '外层函数'def fi():nonlocal info info = info + '1234'print(info) fi()print(info)>>> >>> info'全局'>>> fo()外层函数1234外层函数1234>>> info'全局'>>> >>>
看完上述内容,你们对python中global和nonlocal关键字有什么区别有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注本站行业资讯频道,感谢大家的支持。