为什么函数是原子的? [英] Why are functions atomic?

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

问题描述

为什么函数是原子的? (即它们不被复制。)


例如,我想制作一个函数的副本,这样我就可以更改

的默认值:< br>


>>来自复制导入副本
f = lambda x:x < br / >f.func_defaults =(1,)
g = copy(f)
g.func_defaults =(2,)
f(),g()



(2,2)


我想要以下行为:


>> f(),g()



(1,2)

我知道我可以使用''仿函数'来定义__call__并使用成员

变量,但这更复杂,而且相当慢。 (我

也知道我可以使用new.function来创建一个新副本,但是我想知道做出函数的决定背后的理性
原子在我拍脚之前;-)


谢谢,

迈克尔。

解决方案

Michael写道:


为什么函数是原子的? (即它们不被复制。)


例如,我想制作一个函数的副本,这样我就可以更改

的默认值:< br>


>>>来自复制导入副本
f = lambda x:x
f.func_defaults =(1,)
g = copy(f)
g.func_defaults =(2,)
f(),g()



(2,2)


我想要以下行为:


>>> f(),g()



(1,2)


我知道我可以使用''functor''定义__call__并使用成员

变量,但这更复杂,而且速度稍慢。 (我

也知道我可以使用new.function来创建一个新副本,但是我想知道做出函数的决定背后的理性
原子在我开始自拍之前;-)


谢谢,

迈克尔。



这不会使函数可以复制,但可以解决你的默认参数

问题。在Python 2.5下,请参阅functools.partial()。

http://docs.python.org/lib/partial-objects.html


Laurent Pointal写道:

http://docs.python .org / lib / partial-objects.html



OOps http://docs.python.org/lib/module-functools.html


5月1日凌晨5点06分,Michael< michael.for ... @ gmail.comwrote:


为什么函数是原子的? (即它们不被复制。)


例如,我想制作一个函数的副本,这样我就可以更改

的默认值:< br>


>来自复制导入副本
f = lambda x:x
f.func_defaults =(1, )
g = copy(f)
g.func_defaults =(2,)
f(),g()



(2,2)


我想要以下行为:


> f(),g()



(1,2)


我知道我可以使用''functor''定义__call__并使用成员

变量,但这更复杂,而且速度稍慢。 (我

也知道我可以使用new.function来创建一个新副本,但是我想知道做出函数的决定背后的理性
原子在我开始自拍之前;-)


谢谢,

迈克尔。



深度检查是否有效?


Why are functions atomic? (I.e. they are not copied.)

For example, I would like to make a copy of a function so I can change
the default values:

>>from copy import copy
f = lambda x: x
f.func_defaults = (1,)
g = copy(f)
g.func_defaults = (2,)
f(),g()

(2, 2)

I would like the following behaviour:

>>f(),g()

(1,2)

I know I could use a ''functor'' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)

Thanks,
Michael.

解决方案

Michael wrote:

Why are functions atomic? (I.e. they are not copied.)

For example, I would like to make a copy of a function so I can change
the default values:

>>>from copy import copy
f = lambda x: x
f.func_defaults = (1,)
g = copy(f)
g.func_defaults = (2,)
f(),g()

(2, 2)

I would like the following behaviour:

>>>f(),g()

(1,2)

I know I could use a ''functor'' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)

Thanks,
Michael.

This dont make functions copiable but may resolve your default arguments
problem. Under Python 2.5, see functools.partial().

http://docs.python.org/lib/partial-objects.html


Laurent Pointal wrote:

http://docs.python.org/lib/partial-objects.html

OOps http://docs.python.org/lib/module-functools.html


On May 1, 5:06 am, Michael <michael.for...@gmail.comwrote:

Why are functions atomic? (I.e. they are not copied.)

For example, I would like to make a copy of a function so I can change
the default values:

>from copy import copy
f = lambda x: x
f.func_defaults = (1,)
g = copy(f)
g.func_defaults = (2,)
f(),g()


(2, 2)

I would like the following behaviour:

>f(),g()


(1,2)

I know I could use a ''functor'' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)

Thanks,
Michael.

Does deepcopy work?


这篇关于为什么函数是原子的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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