Python 中巢狀函式是如何工作的?
在本文中,我們將解釋 Python 中的巢狀/內部函式以及它們如何透過示例工作。
巢狀(或內部)函式是在其他函式內定義的函式,它們允許我們直接訪問封閉函式中定義的變數和名稱。巢狀函式可用於建立閉包和裝飾器等。
定義內部/巢狀函式
只需使用 def 關鍵字在函式內初始化另一個函式即可定義一個巢狀函式。
以下程式演示了 Python 中的內部函式:
示例
# creating an outer function def outerFunc(sample_text): sample_text = sample_text # creating an inner function def innerFunc(): # Printing the variable of the parent class(outer class) print(sample_text) # calling inner function innerFunc() # Calling the outer Function by passing some random name outerFunc('Hello tutorialspoint python')
輸出
執行上述程式後,將生成以下輸出:
Hello tutorialspoint python
這裡,innerFunc() 在 outerFunc() 內部宣告,使其成為一個內部函式。在呼叫 innerFunc() 之前,我們必須先呼叫 outerFunc()。然後,outerFunc() 將呼叫在其內部定義的 innerFunc()。
必須呼叫外部函式才能執行內部函式。
不呼叫外部函式
示例
# creating an outer function def outerFunc(sample_text): sample_text = sample_text # creating an inner function def innerFunc(): print(sample_text) # calling inner function innerFunc()
輸出
執行上述程式後,將生成以下輸出:
執行上述程式碼時,將不會返回任何結果。
巢狀函式中變數的作用域
變數的作用域是我們可以在其中找到變數並在必要時訪問它的位置。
如何在函式內訪問全域性變數是眾所周知的,但如何訪問外部函式的變數呢?
以下程式演示了巢狀函式的作用域:
示例
# creating an outer function def outerFunc(): sample_str = 'Hello tutorialspoint python' # creating an inner function def innerFunc(): print(sample_str) # calling an inner function inside the outer function innerFunc() # calling outer function outerFunc()
輸出
執行上述程式後,將生成以下輸出:
Hello tutorialspoint python
從以上示例可以清楚地看出,它等效於從函式訪問全域性變數。
假設您希望修改外部函式的變數。
示例
以下程式更改巢狀函式(內部函式)內變數的值:
# creating an outer function def outerFunc(): sample_str = 'Hello tutorialspoint python' # creating an inner function def innerFunc(): # modifying the sample string sample_str = 'Welcome' print(sample_str) # calling an inner function inside the outer function innerFunc() print(sample_str) # calling outer function outerFunc()
輸出
執行上述程式後,將生成以下輸出:
Welcome Hello tutorialspoint python
這裡,外部函式的變數值保持不變。但是,可以修改外部函式的變數值。有多種方法可以更改外部函式的變數值。
使用內部函式的可迭代物件
以下程式演示瞭如何在內部函式中修改可迭代物件:
# Creating outer function def outerFunc(): # Creating an iterable sample_str = ['Hello tutorialspoint python'] # Creating inner Function/Nested Function def innerFunc(): # using an iterable to modify the value sample_str[0] = 'Welcome' print(sample_str) # Calling the inner function innerFunc() # Printing variable of the outer function print(sample_str) # Calling the outer function outerFunc()
輸出
執行上述程式後,將生成以下輸出:
['Welcome'] ['Welcome']
為什麼要使用巢狀函式?
封裝和閉包/工廠函式是使用巢狀函式最常見的兩個原因。
資料封裝
在某些情況下,您希望封裝函式或其可以訪問的資料,以防止從程式碼的其他部分訪問它。
當您像這樣巢狀函式時,它對全域性作用域隱藏起來。由於此特性,資料封裝也稱為資料隱藏或資料隱私。
# creating an outer function def outerFunc(): print("This is outer function") # creating an inner function def innerFunc(): print("This is inner function") # calling an inner function inside the outer function innerFunc() # calling inner function outside the outer function innerFunc()
輸出
執行上述程式後,將生成以下輸出:
Traceback (most recent call last): File "main.py", line 11, in <module> innerFunc() NameError: name 'innerFunc' is not defined
上面程式碼中的內部函式只能從外部函式內部訪問。如果嘗試從外部函式外部呼叫內部函式,您將看到上面顯示的錯誤。
相反,您必須按如下方式呼叫外部函式。
# creating an outer function def outerFunc(): print("This is outer function") # creating an inner function def innerFunc(): print("This is inner function") # calling an inner function inside the outer function innerFunc() # calling outer function outerFunc()
輸出
執行上述程式後,將生成以下輸出:
This is outer function This is inner function
閉包
但是,如果外部函式返回內部函式本身,而不是像前面的示例那樣呼叫它呢?在這種情況下,您將擁有稱為閉包的內容。
在 Python 中建立閉包必須滿足以下條件。
需要一個巢狀函式。
內部函式必須引用在封閉作用域中定義的值。
巢狀函式必須由封閉函式返回。
示例
def number_1(a): def number_2(b): return a*b return number_2 print(number_1(3)(2))
輸出
執行上述程式後,將生成以下輸出:
6
閉包允許您將資料傳遞給內部函式,而無需先使用引數將其傳遞給外部函式。它們還允許從封裝外部函式外部呼叫內部函式。所有這些都具有前面提到的資料封裝/隱藏的優勢。
結論
本文向我們介紹了內部函式、巢狀函式的目的、它們的工作原理以及 Python 中巢狀函式的作用域。
從這裡開始學習 Python:Python 教程