欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Python3中global/nonlocal用法

发布时间:2023/11/27 64 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python3中global/nonlocal用法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

      全局变量(global variable)是那些未在任何函数内部定义并且具有全局作用域的变量,而局部变量(local variable)是那些在函数内部定义并且其作用域仅限于该函数的变量。换句话说,我们可以说局部变量只能在初始化它的函数内部访问,而全局变量在整个程序和每个函数内部都可以访问。

      global关键字:如果我们要进行赋值或更改全局变量,我们只需要在函数中使用global关键字。打印和访问全局变量不需要global关键字。如果在函数内部更改或创建的任何变量尚未声明为全局变量,则它都是局部变量。在函数外使用global关键字无效。global绑定全局变量。

      nonlocal关键字:与global关键字功能相似,但用于嵌套函数(nested function)中。nonlocal绑定局部变量,只对局部起作用,离开嵌套函数,那么该变量则无效。

      以上内容主要参考:https://www.geeksforgeeks.org/global-local-variables-python/

      以下为测试代码:

var = 6if var == 1:# reference: https://www.geeksforgeeks.org/global-local-variables-python/def f(): # Creating local variabless = "I love Geeksforgeeks" # local vairableprint("Inside Function:", s)f()#print("s:", s) # NameError: name 's' is not defined
elif var == 2:# reference: https://www.geeksforgeeks.org/global-local-variables-python/# Defining and accessing global variablesdef f(): # This function uses global variable sprint("Inside Function:", s)# Global scopes = "I love Geeksforgeeks" # global variable,  is used both inside the f function as well as outside the f functionf()print("Outside Function:", s)
elif var == 3:# reference: https://www.geeksforgeeks.org/global-local-variables-python/# This function has a variable with name same as sdef f():#s += 'GFG' # UnboundLocalError: local variable 's' referenced before assignments = "Me too." # 如果在函数作用域内也定义了同名变量,那么它将仅打印函数内部给出的值,而不是全局值print(s)s = "I love Geeksforgeeks" # global scopef()print(s) # I love Geeksforgeeks
elif var == 4:# reference: https://www.geeksforgeeks.org/global-local-variables-python/# This function modifies the global variable 's'def f():global s # 如果我们要进行赋值或更改全局变量,我们只需要在函数中使用global关键字s += ' GFG'print(s)s = "Look for Geeksforgeeks Python Section"print(s)s = "Python is great!" # global scopef()print(s) # Look for Geeksforgeeks Python Section
elif var == 5:# reference: https://www.geeksforgeeks.org/use-of-nonlocal-vs-use-of-global-keyword-in-python/def fun():var1 = 10def gun():nonlocal var1 # tell python explicitly that it has to access var1 initialized in fun using the keyword nonlocal# global var1; var1 = var1 + 10 # NameError: name 'var1' is not definedvar1 = var1 + 10print(var1) # 20gun()print(var1) # 20fun()
elif var == 6:# reference: https://www.geeksforgeeks.org/python-nonlocal-keyword/def geek_func():geek_name = "geekforgeeks" # local variable to geek_funcdef geek_func1(): # First Inner functiongeek_name = "GeekforGeeks"def geek_func2(): # Second Inner functionnonlocal geek_name # Declairing nonlocal variablegeek_name = 'GEEKSFORGEEKS'print(geek_name) # Printing our nonlocal variable, GEEKSFORGEEKSgeek_func2() # Calling Second inner functiongeek_func1() # Calling First inner functionprint(geek_name) # Printing local variable to geek_func, geekforgeeksgeek_func()
print("test finish")

      GitHub:https://github.com/fengbingchun/Python_Test

总结

以上是生活随笔为你收集整理的Python3中global/nonlocal用法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。