什么是python闭包真的像? [英] What are python closures realy like?

查看:59
本文介绍了什么是python闭包真的像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在编写我的上一个程序时遇到了问题

通过一堆

函数访问公共局部变量。

我想要一个函数,根据

一些参数,返回其他函数都可以访问

相同的变量。一个OO方法会做,但为什么不

试用闭包......

所以这里有一个简单的例子:

def fun_basket (f):

common_var = [0]

def f1():

print common_var [0]

common_var [0] = 1

def f2():

print common_var [0]

common_var [0] = 2
如果f == 1:

返回f1

如果f == 2:

返回f2

如果你从fun_basket的内部调用f1和f2,它们的行为与预期的一样,所以common_var [0]被修改为

无论函数在哪个函数上运行。

但是,调用f1 = fun_basket(1); f2 = fun_basket(2)和

然后是f1(); f2()返回0和0.这不是人们希望闭包工作的方式,例如, Lisp制作计数器。

任何想法在幕后发生了什么?

Hi,
while writing my last program I came upon the problem
of accessing a common local variable by a bunch of
functions.
I wanted to have a function which would, depending on
some argument, return other functions all having access to
the same variable. An OO approach would do but why not
try out closures...
So here is a simplified example of the idea:
def fun_basket(f):
common_var = [0]
def f1():
print common_var[0]
common_var[0]=1
def f2():
print common_var[0]
common_var[0]=2
if f == 1:
return f1
if f == 2:
return f2
If you call f1 and f2 from the inside of fun_basket, they
behave as expected, so common_var[0] is modified by
whatever function operates on it.
However, calling f1=fun_basket(1); f2 = fun_basket(2) and
then f1(); f2() returns 0 and 0. It is not the way one would
expect closures to work, knowing e.g. Lisp make-counter.
Any ideas what''s going on behind the scene?

推荐答案

12/1 / 06,Karl Kofnarson< ko ******* @ gmail.comwrote:

[snip]
On 12/1/06, Karl Kofnarson <ko*******@gmail.comwrote:
[snip]

def fun_basket( f):

common_var = [0]

def f1():

print common_var [0]

common_var [0] = 1

def f2():

print common_var [0]

common_var [0] = 2

如果f == 1:

返回f1

如果f == 2:

返回f2
def fun_basket(f):
common_var = [0]
def f1():
print common_var[0]
common_var[0]=1
def f2():
print common_var[0]
common_var[0]=2
if f == 1:
return f1
if f == 2:
return f2



每次调用fun_basket时都会创建另一个common_var。

Everytime you call fun_basket you create another common_var.


但是,调用f1 = fun_basket(1); f2 = fun_basket(2)和

然后是f1(); f2()返回0和0.
However, calling f1=fun_basket(1); f2 = fun_basket(2) and
then f1(); f2() returns 0 and 0.



两个调用fun_basket,两个不同的common_var',两个f1'和两个

f2'。每个f1 / f2对都可以访问不同的common_var,因此它的工作符合预期。为了按预期工作,fun_basket应该在

上定义相同的块common_var。


-

Felipe。

Two calls to fun_basket, two different common_var''s, two f1''s and two
f2''s. Each f1/f2 pair have access to a different common_var, so it''s
working as expected. To work as you expected, fun_basket should be on
the same block common_var is defined.

--
Felipe.


Karl Kofnarson写道:
Karl Kofnarson wrote:



在写我的最后一个节目的时候我遇到问题

通过一堆

函数访问一个公共局部变量。

我想要一个函数,这将取决于在

一些参数,返回其他函数都可以访问

相同的变量。一个OO方法会做,但为什么不

试用闭包......

所以这里有一个简单的例子:

def fun_basket (f):

common_var = [0]

def f1():

print common_var [0]

common_var [0] = 1

def f2():

print common_var [0]

common_var [0] = 2
如果f == 1:

返回f1

如果f == 2:

返回f2

如果你从fun_basket的内部调用f1和f2,它们的行为与预期的一样,所以common_var [0]被修改为

无论函数在哪个函数上运行。

但是,调用f1 = fun_basket(1); f2 = fun_basket(2)和

然后是f1(); f2()返回0和0.这不是人们希望闭包工作的方式,例如, Lisp制作柜台。

任何想法在幕后发生了什么?
Hi,
while writing my last program I came upon the problem
of accessing a common local variable by a bunch of
functions.
I wanted to have a function which would, depending on
some argument, return other functions all having access to
the same variable. An OO approach would do but why not
try out closures...
So here is a simplified example of the idea:
def fun_basket(f):
common_var = [0]
def f1():
print common_var[0]
common_var[0]=1
def f2():
print common_var[0]
common_var[0]=2
if f == 1:
return f1
if f == 2:
return f2
If you call f1 and f2 from the inside of fun_basket, they
behave as expected, so common_var[0] is modified by
whatever function operates on it.
However, calling f1=fun_basket(1); f2 = fun_basket(2) and
then f1(); f2() returns 0 and 0. It is not the way one would
expect closures to work, knowing e.g. Lisp make-counter.
Any ideas what''s going on behind the scene?



Python可以完全按字面读取。 " common_var"是fun_basket的局部变量

,因此它独立于fun_basket的invokations。

" def"是一个在执行时创建函数的语句。如果

你执行两次相同的def语句,则会创建两个不同的函数

。运行fun_basket两次创建四个闭包,第一个

两个与后两个没有关系。这两组关闭了

不同的单元变量。


如果你想在函数调用之间共享数据,你需要一个

在调用之间持续存在的对象。您可以使用全局变量,或者使用
作为默认参数。但由于每次调用

函数时都会共享该值,因此我没有看到使用闭包的价值。我不会很好地了解lisp,但在我看来,闭包的重点是

你每次都可以引用一个不同的独特单元格。


-MIke

Python can be read quite literally. "common_var" is a local variable
to fun_basket, hence it independent among invokations of fun_basket.
"def" is a statement that creates a function when it is executed. If
you execute the same def statement twice, two different functions are
created. Running fun_basket twice creates four closures, and the first
two have no relation to the second two. The two sets close over
different cell variables.

If you want to share data between function invokation, you need an
object which persists between calls. You can use a global variable, or
a default argument. But since the value is shared everytime the
function is called, I don''t see the value in using a closure. I don''t
know lisp very well, but in my mind the whole point of closures is that
you can reference a different unique cell each time.

-MIke


" Karl Kofnarson" < ko ******* @ gmail.com在留言中写道

新闻:pa *********************** ***** @ gmail.com ...
"Karl Kofnarson" <ko*******@gmail.comwrote in message
news:pa****************************@gmail.com...



在编写我的上一个程序时我遇到了问题

通过一堆

函数访问一个公共局部变量。

我想要一个函数,这取决于

一些参数,返回其他函数都可以访问

相同的变量。一个OO方法会做,但为什么不

试用闭包......

所以这里有一个简单的例子:

def fun_basket (f):

common_var = [0]

def f1():

print common_var [0]

common_var [0] = 1

def f2():

print common_var [0]

common_var [0] = 2
如果f == 1:

返回f1

如果f == 2:

返回f2
Hi,
while writing my last program I came upon the problem
of accessing a common local variable by a bunch of
functions.
I wanted to have a function which would, depending on
some argument, return other functions all having access to
the same variable. An OO approach would do but why not
try out closures...
So here is a simplified example of the idea:
def fun_basket(f):
common_var = [0]
def f1():
print common_var[0]
common_var[0]=1
def f2():
print common_var[0]
common_var[0]=2
if f == 1:
return f1
if f == 2:
return f2



Karl,


通常在使用这个成语时,fun_basket会返回所有

定义函数的元组而不是一个与另一个。因此代替:

Karl,

Usually when using this idiom, fun_basket would return a tuple of all of the
defined functions, rather than one vs. the other. So in place of:


如果f == 1:

返回f1

如果f == 2 :

返回f2
if f == 1:
return f1
if f == 2:
return f2



只做

Just do


返回f1,f2
return f1, f2



(就此而言,不再需要参数f。)


然后你的来电者将得到2个函数,谁有共同的变种。你不会再打电话给fun_basket,你已经创建了两个闭包。打电话

fun_basket使用类似的东西:


z1,z2 = fun_basket(无)


然后拨打z1 ()和z2()在闲暇时 - 他们应该有所需的行为。


- Paul

(For that matter, the argument f is no longer needed either.)

Then your caller will get 2 functions, who share a common var. You don''t
call fun_basket any more, you''ve already created your two "closures". Call
fun_basket using something like:

z1,z2 = fun_basket(None)

And then call z1() and z2() at your leisure - they should have the desired
behavior.

-- Paul


这篇关于什么是python闭包真的像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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