Questions tagged «nested-function»

7
为什么python嵌套函数不称为闭包?
我已经在Python中看到并使用了嵌套函数,它们与闭包的定义匹配。那么为什么叫他们nested functions而不是closures? 嵌套函数不是因为没有被外部世界使用而不是闭包吗? 更新:我正在阅读有关闭包的知识,这让我开始思考关于Python的这个概念。我搜索并找到了某人在下面的评论中提到的文章,但是我无法完全理解该文章中的解释,所以这就是为什么我问这个问题。

4
嵌套函数中的局部变量
好的,请耐心等待,我知道它看起来会令人费解,但是请帮助我了解发生了什么。 from functools import partial class Cage(object): def __init__(self, animal): self.animal = animal def gotimes(do_the_petting): do_the_petting() def get_petters(): for animal in ['cow', 'dog', 'cat']: cage = Cage(animal) def pet_function(): print "Mary pets the " + cage.animal + "." yield (animal, partial(gotimes, pet_function)) funs = list(get_petters()) for name, f in funs: …

7
JavaScript嵌套函数
我有一段我不明白的javascript代码: function dmy(d) { function pad2(n) { return (n < 10) ? '0' + n : n; } return pad2(d.getUTCDate()) + '/' + pad2(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear(); } function outerFunc(base) { var punc = "!"; //inner function function returnString(ext) { return base + ext + punc; } …

6
Python中的嵌套函数
像这样的Python代码可以给我们带来什么好处或启示: class some_class(parent_class): def doOp(self, x, y): def add(x, y): return x + y return add(x, y) 我在一个开源项目中发现了这一点,它在嵌套函数内部做了一些有用的事情,但是除了调用它之外,什么也没做。(可以在此处找到实际的代码。)为什么有人会这样编码?在嵌套函数内部而不是外部普通函数中编写代码是否有好处或副作用?

12
什么是PHP嵌套函数?
在JavaScript中,嵌套函数非常有用:闭包,私有方法以及您拥有的东西。 什么是嵌套PHP函数?有人使用它们吗? 这是我做的小调查 <?php function outer( $msg ) { function inner( $msg ) { echo 'inner: '.$msg.' '; } echo 'outer: '.$msg.' '; inner( $msg ); } inner( 'test1' ); // Fatal error: Call to undefined function inner() outer( 'test2' ); // outer: test2 inner: test2 inner( 'test3' ); // …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.