Python 中的嵌套函数
使用这样的 Python 代码我们可以得到什么好处或含义:
class some_class(parent_class):
def doOp(self, x, y):
def add(x, y):
return x + y
return add(x, y)
我在一个开源项目中发现了这一点,在嵌套函数内部做了一些有用的事情,但除了调用它之外,在外部绝对不做任何事情。 (实际代码可以找到这里。)为什么有人会这样编码?在嵌套函数内部而不是在外部普通函数中编写代码是否有一些好处或副作用?
What benefit or implications could we get with Python code like this:
class some_class(parent_class):
def doOp(self, x, y):
def add(x, y):
return x + y
return add(x, y)
I found this in an open-source project, doing something useful inside the nested function, but doing absolutely nothing outside it except calling it. (The actual code can be found here.) Why might someone code it like this? Is there some benefit or side effect for writing the code inside the nested function rather than in the outer, normal function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
通常,您这样做是为了创建闭包:
内部函数可以访问封闭范围内的变量(在本例中为局部变量
x
)。如果您没有从封闭范围访问任何变量,那么它们实际上只是具有不同范围的普通函数。Normally you do it to make closures:
Inner functions can access variables from the enclosing scope (in this case, the local variable
x
). If you're not accessing any variables from the enclosing scope, they're really just ordinary functions with a different scope.除了函数生成器之外,内部函数的创建几乎就是函数生成器的定义,我创建嵌套函数的原因是为了提高可读性。如果我有一个仅由外部函数调用的小函数,那么我会内联该定义,这样您就不必跳过来确定该函数正在做什么。如果我发现以后需要重用该函数,我总是可以将内部方法移到封装方法之外。
玩具示例:
Aside from function generators, where internal function creation is almost the definition of a function generator, the reason I create nested functions is to improve readability. If I have a tiny function that will only be invoked by the outer function, then I inline the definition so you don't have to skip around to determine what that function is doing. I can always move the inner method outside of the encapsulating method if I find a need to reuse the function at a later date.
Toy example:
使用内部方法的一个潜在好处是,它允许您使用外部方法局部变量,而无需将它们作为参数传递。
可以写成如下,可以说读起来更好
One potential benefit of using inner methods is that it allows you to use outer method local variables without passing them as arguments.
can be written as follows, which arguably reads better
我无法想象这样的代码有什么好的理由。
也许旧版本中的内部功能是有原因的,就像其他操作一样。
例如,这使得稍微更有意义:
但是内部函数应该是(“私有”)类方法:
I can't image any good reason for code like that.
Maybe there was a reason for the inner function in older revisions, like other Ops.
For example, this makes slightly more sense:
but then the inner function should be ("private") class methods instead:
本地方法背后的想法与本地变量类似:不要污染更大的名称空间。显然,好处是有限的,因为大多数语言也不直接提供此类功能。
The idea behind local methods is similar to local variables: don't pollute the larger name space. Obviously the benefits are limited since most languages don't also provide such functionality directly.
你确定代码是这样的吗?做这样的事情的正常原因是创建一个部分 - 一个带有内置参数的函数。调用外部函数会返回一个不需要参数的可调用对象,因此可以在无法传递参数的地方存储和使用。但是,您发布的代码不会执行此操作 - 它立即调用该函数并返回结果,而不是可调用的。发布您看到的实际代码可能会很有用。
Are you sure the code was exactly like this? The normal reason for doing something like this is for creating a partial - a function with baked-in parameters. Calling the outer function returns a callable that needs no parameters, and so therefore can be stored and used somewhere it is impossible to pass parameters. However, the code you've posted won't do that - it calls the function immediately and returns the result, rather than the callable. It might be useful to post the actual code you saw.
在Python中,您可以使用嵌套函数来创建装饰器,例如
@decorator
。 *我的回答 解释了有关装饰器的更多信息。我创建了
multiply_by_5()
来将其用作sum()
的装饰器,如下所示:输出:
下面的代码是不使用装饰器的情况:
或者:
输出:
In Python, you can use a nested function to create a decorator like
@decorator
. *My answer explains more about decorators.I created
multiply_by_5()
to use it as the decorator forsum()
as shown below:Output:
The code below is the case of not using the decorator:
Or:
Output: