English 中文(简体)
• 如何迫使第3号 p子通过价值?
原标题:How to force python3 to pass by value?

我的用意是建立一个字典,其钥匙是原始的,其价值是零点功能,可以恢复。 (这是实施一个多国公司大型项目的一部分) 其中一些职能是非属地的,是人工设定和分配的。 这些工作罚款。 然而,另一些则似乎可以自动生成。

我的第一次尝试失败了:

>>> regs = [ a ,  b ,  c ,  x ,  y ,  z ]
>>> vals = {i : lambda: r for i, r in enumerate(regs)}
>>> [(k, vals[k]()) for k in vals.keys()]
[(0,  z ), (1,  z ), (2,  z ), (3,  z ), (4,  z ), (5,  z )]

缩略语 我再次试图孤立自己的价值:

>>> from copy import copy
>>> vals = {}
>>> i = 0
>>> for reg in regs:
...     r = copy(reg)  # (1)
...     vals[i] = lambda: r
...     i += 1
...
>>> [(k, vals[k]()) for k in vals.keys()]
[(0,  z ), (1,  z ), (2,  z ), (3,  z ), (4,  z ), (5,  z )]

(1) 我认为,这一步骤将产生一个独立变量,在进行管制时不会改变。 情况并非如此。

因此,这一尝试显然无所作为。 或许可以复制一下,看上去吗?

>>>  s  is  s 
True
>>> a =  s 
>>> b = copy(a)
>>> a is b
True
>>> from copy import deepcopy
>>> b = deepcopy(a)
>>> a is b
True

权利。 插图是一纸空文。 深层的斜体就是这样。 因此,Mlambda仍然提到正在更新的每个 lo体变量,造成这种ug。

We need a different approach. What if I save the variable I want to a static variable of the temporary function? That should work if each temporary function gets its own identity...

>>> vals = {}
>>> i = 0
>>> for reg in regs:
...     def t():
...             return t.r
...     t.r = reg
...     vals[i] = t
...     i += 1
...
>>> [(k, vals[k]()) for k in vals.keys()]
[(0,  z ), (1,  z ), (2,  z ), (3,  z ), (4,  z ), (5,  z )]

Nope。 此时此刻,我 m忙地处理:

>>> vals = {}
>>> vals[0] = lambda:  a 
>>> vals[1] = lambda:  b 

......等等。 然而,这感觉是放弃,而且令人难以置信。 是否为完成这项任务提供了适当的onic? 毕竟,我通常热爱的神话之一是,我离开了人工点管理;我从未想象过,我祝愿它包括一整套点器工具!

最佳回答

Closures never copy, they copy neither values nor references. Instead, they remember the scope and variable they use, and always go back there. The same is true in several other languages, such as C#, JavaScript, IIRC Lisp and probably more. The same is true in several other languages. This is important for some advanced use cases (basically every time you want several closures to share state), but it can bite one. For instance:

x = 1
def f(): return x
x = 2
assert f() == 2

由于“灰色”只为职能(以及类别和单元)创造了新的范围,但这里确实如此,因此,只有一度存在“样形形”的“条码>,因此所有封闭都指相同的变数。 因此,在休息之后,他们看到变数假设的最后价值。

情况也是如此,只有这样,才有共享的t——你确实设立了单独的封闭区,每个区都有其r属性中的正确价值。 但是,当有人要求时,他们将“t”放在封面的范围之内,因此总是提到你最后造成的关闭。

有几个工作领域。 一种做法是将封锁的建立推向一种单独的功能,迫使每次关闭都有一个新的专用范围,以提及:

def make_const(x):
    def const():
        return x
    return const

另一种可能性(主人,但更模糊)是(a) 援引违约参数在定义时受到约束的事实:

for reg in regs:
    t = lambda reg=reg: reg

在其他情况下,您可以使用<代码>functools,但似乎在此不适用。

问题回答

暂无回答




相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签