在Python中,當引用一個變數的時候,對這個變數的搜尋是按找本地作用域(Local)、嵌套作用域(Enclosing function locals)、全域作用域(Global)、內建作用域(builtins模組)的順序來進行的,即所謂的LEGB規則。即python引用變數的順序: 目前作用域局部變數->外層作用域變數->目前模組中的全域變數->python內建變數。
然而當在一個函數內部為一個變數賦值時,並不是按照上面所說LEGB規則來先找到變量,之後就為該變數賦值。在Python中,在函數中為一個變數賦值時,有下面這樣一條規則:「當在函數中給一個變數名賦值是(而不是在一個表達式中對其進行引用),Python總是創建或改變本地作用域的變數名,除非它已經在那個函數中被宣告為全域變數。」
python中golbal全域變數和nonlocal局部變數
global關鍵字用來在函數或其他局部作用域中使用全域變數。
gcount = 0 #定义了一个全局变量,(可以省略global关键字) def global_test(): print (gcount) #不修改,只是引用全局变量,不使用global关键字 def global_counter(): global gcount gcount +=1 #修改全局变量,需要使用global关键字 return gcount
輸出
1
#2
3
nonlocal關鍵字是用來在函數或其他作用域中使用外層(非全域)變數。
def make_counter(): count = 0 #定义一个局部变量,该局部变量作用范围在make_counter()方法内 def counter(): nonlocal count #在方法counter()中使用外层局部变量 count count += 1 return count return counter def make_counter_test(): mc = make_counter() print(mc()) print(mc()) print(mc()) make_counter_test()
輸出
3
#以上是python如何在非創建全域變數的地方使用全域變量的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

WebStorm Mac版
好用的JavaScript開發工具