当Closure获得外部变量的值? [英] When Closure get external variable's value?

查看:75
本文介绍了当Closure获得外部变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码会打印什么? (10或15)


def testClosure(maxIndex):


def closureTest():

返回maxIndex


maxIndex + = 5

return closureTest()


print testClosure(10)


我的问题是闭包函数何时获得maxindex的值?运行

时间或编译时间?


谢谢。

What will the following piece of code print? (10 or 15)

def testClosure(maxIndex) :

def closureTest():
return maxIndex

maxIndex += 5

return closureTest()

print testClosure(10)

My question is when the closure function gets value for maxindex? Run
time or compile time?

Thanks.

推荐答案

它将打印15.闭包在运行时获取值。


我们可以将闭包视为外部函数的一部分并且它共享

具有持有功能的局部变量?

It will print 15. The closure gets the value at run time.

Could we treat closure as part of the external function and it shares
the local variable with its holder function?


华阳夏kirjoitti:
Huayang Xia kirjoitti:

它将打印15.关闭在运行时获取值。


我们可以将闭包视为外部函数的一部分,并且它与其持有者共享局部变量
功能?
It will print 15. The closure gets the value at run time.

Could we treat closure as part of the external function and it shares
the local variable with its holder function?



我不太明白你想要告诉我们的内容,但如果你认为你的示例代码中有



def testClosure(maxIndex):


def closureTest():

返回maxIndex


maxIndex + = 5


返回closureTest()


打印testClosure (10)


你返回一个可调用的函数你错了。这可以很容易看到:

I don''t quite get what you are trying to tell us but if you think that
in your example code:

def testClosure(maxIndex) :

def closureTest():
return maxIndex

maxIndex += 5

return closureTest()

print testClosure(10)

you are returning a callable function you are all wrong. This can be
easily seen by:


>> ; type(testClosure(10))
>>type(testClosure(10))



15

< type''int''> ;


错误的是你不应该返回closureTest()而是closeTest

。正确的方法是:

15
<type ''int''>

The mistake is that you shouldn''t return closureTest() but closureTest
instead. The correct way would be:


>> def testClosure2(maxIndex):
>>def testClosure2(maxIndex):



def closureTest():

返回maxIndex

maxIndex + = 5

返回closureTest

def closureTest():
return maxIndex
maxIndex += 5
return closureTest


>> f2 = testClosure2(10 )
>>f2 = testClosure2(10)



< function closureTest at 0x00D82530>

<function closureTest at 0x00D82530>


>> type(f2)
>>type(f2)



< type''function''>

<type ''function''>


>> print f2()
>>print f2()



15


干杯,

Jussi

15

Cheers,
Jussi


感谢您的澄清。


但我的问题是:


什么时候闭包得到以下maxIndex的值

代码片段?

def testClosure(maxIndex):


def closureTest():

返回maxIndex


maxIndex + = 5

return closureTest()


打印testClosure(10)

我认为它应该是10而不是15.这是错误的。


经过多次测试,我发现maxIndex是,但是,本地到

testClosure()但是在closureTest()的外部。 closureTest()获取运行时maxIndex值的
。所以它是15而不是10.

以下片段将进一步验证:


def testClosure1(lst):


def closureTest():

lst.append(lst [-1] +1)


lst.append(lst [ -1] +1)

返回closureTest()


alist = [1]

testClosure1(alist)

alist.append(3)

testClosure1(alist)


函数testClosure1()和闭包测试中的''lst'' ()与alist相同的是
。所以一切都是动态的。变量的值是在运行时确定的


Thanks for the clarification.

But my question is:

When does the closure get the value of the maxIndex in the following
code snippet?

def testClosure(maxIndex) :

def closureTest():
return maxIndex

maxIndex += 5

return closureTest()

print testClosure(10)
I thought it should be 10 instead of 15. That was wrong.

After several tests, I found maxIndex is, though, local to
testClosure() but is external to the closureTest(). closureTest() gets
the value of maxIndex at run time. So that it''s 15 instead of 10. The
following snippet will verify that further:

def testClosure1(lst):

def closureTest():
lst.append(lst[-1]+1)

lst.append(lst[-1]+1)
return closureTest()

alist = [1]
testClosure1(alist)
alist.append(3)
testClosure1(alist)

The ''lst'' in function testClosure1() and the closure closureTest() are
same thing as alist. So everything is dynamic. Variable''s value is
determined at run time.


这篇关于当Closure获得外部变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆