标签: nested-function

为什么不将python嵌套函数称为闭包?

我已经在Python中看到并使用了嵌套函数,它们与闭包的定义相匹配.那他们为什么叫nested functions而不是closures

嵌套函数是不是闭包,因为它们不被外部世界使用?

更新:我正在阅读关于闭包的内容,这让我想到了关于Python的这个概念.我在下面的评论中搜索并找到了某人提到的文章,但我无法完全理解该文章中的解释,所以这就是我提出这个问题的原因.

python closures nested-function

234
推荐指数
6
解决办法
9万
查看次数

嵌套函数中的局部变量

好吧,请耐心等待我,我知道这看起来会非常令人费解,但请帮助我了解发生了什么.

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:
    print name + ":", 
    f()
Run Code Online (Sandbox Code Playgroud)

得到:

cow: Mary pets the cat.
dog: Mary pets the cat.
cat: Mary pets the cat.
Run Code Online (Sandbox Code Playgroud)

所以基本上,为什么我没有得到三种不同的动物?是不是cage'打包'进入嵌套函数的局部范围?如果没有,对嵌套函数的调用如何查找局部变量?

我知道遇到这些问题通常意味着一个人"做错了",但我想了解会发生什么.

python closures scope nested-function

102
推荐指数
3
解决办法
9449
查看次数

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;
    }

    return returnString;
}
Run Code Online (Sandbox Code Playgroud)

如何在另一个函数中定义函数?我们可以从my()函数外部调用pad2()吗?

请点亮一下.谢谢

javascript nested-function

91
推荐指数
4
解决办法
18万
查看次数

Python中的嵌套函数

使用这样的Python代码可以获得什么好处或含义:

class some_class(parent_class):
    def doOp(self, x, y):
        def add(x, y):
            return x + y
        return add(x, y)
Run Code Online (Sandbox Code Playgroud)

我在一个开源项目中发现了这一点,在嵌套函数中做了一些有用的事情,但除了调用它之外什么都不做.(实际的代码可以在这里找到.)为什么有人会像这样编码?在嵌套函数中编写代码而不是在外部正常函数中是否有一些好处或副作用?

python nested-function

78
推荐指数
5
解决办法
8万
查看次数

什么是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' );  // inner: test3
outer( 'test4' );  // Fatal error:  Cannot redeclare inner()
Run Code Online (Sandbox Code Playgroud)

php nested-function

76
推荐指数
5
解决办法
5万
查看次数

为什么 std::fs::write(...) 使用内部函数?

我是 Rust 新手,浏览了一下源代码,发现了这个:

#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
    fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
        File::create(path)?.write_all(contents)
    }
    inner(path.as_ref(), contents.as_ref())
}
Run Code Online (Sandbox Code Playgroud)

这个函数有什么理由定义这样的内部函数吗?为什么不直接写:

File::create(path.as_ref())?.write_all(contents.as_ref())
Run Code Online (Sandbox Code Playgroud)

file-io nested-function rust

62
推荐指数
1
解决办法
3266
查看次数

嵌套函数如何在Python中运行?

def maker(n):
    def action(x):
        return x ** n
    return action

f = maker(2)
print(f)
print(f(3))
print(f(4))

g = maker(3)
print(g(3))

print(f(3)) # still remembers 2
Run Code Online (Sandbox Code Playgroud)

为什么嵌套函数会记住第一个值,2即使maker()已经返回并退出时action()被调用?

python closures nested function nested-function

56
推荐指数
5
解决办法
6万
查看次数

Javascript调用嵌套函数

我有以下代码:

function initValidation()
{
    // irrelevant code here
    function validate(_block){
        // code here
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以在validate()函数外面调用initValidation()函数吗?我试过调用validate()但我认为它只在父函数中可见.

javascript nested-function

32
推荐指数
3
解决办法
5万
查看次数

在C++中模拟嵌套函数

在C中,以下代码可以考虑我总是使用gcc.

int foo( int foo_var )
{
 /*code*/
  int bar( int bar_var )  
  {
    /*code*/
    return bar_var;
  }
  return bar(foo_var);
}
Run Code Online (Sandbox Code Playgroud)

如何在gcc编译器上实现C++中嵌套函数的相同功能?不介意这看起来像是一个初学者的问题.我是这个网站的新手.

c c++ nested-function

27
推荐指数
4
解决办法
2万
查看次数

功能内部功能 - 每次?

我们有这个代码:

def big_function():
    def little_function():
         .......
    .........
Run Code Online (Sandbox Code Playgroud)

Python文档说明了def声明:

函数定义是可执行语句.它的执行绑定了函数名...

所以,问题是:def little_function()每次big_function调用时都会执行吗?问题是关于def陈述的确切,而不是little_function()身体.

python performance closures nested-function

27
推荐指数
2
解决办法
658
查看次数

标签 统计

nested-function ×10

python ×5

closures ×4

javascript ×2

c ×1

c++ ×1

file-io ×1

function ×1

nested ×1

performance ×1

php ×1

rust ×1

scope ×1