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() 内声明,使其成为内部函数。我们必须先调用 outerFunc(),然后才能调用 innerFunc()。然后 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 教程