日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python3中global/nonlocal用法

發布時間:2023/11/27 生活经验 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。