在很多需要它的类中,我一直在使用这种复制方法.
class population (list):
def __init__ (self):
pass
def copy(self):
return copy.deepcopy(self)
Run Code Online (Sandbox Code Playgroud)
它突然开始产生这个错误:
File "C:\Python26\lib\copy.py", line 338, in _reconstruct
state = deepcopy(state, memo)
File "C:\Python26\lib\copy.py", line 162, in deepcopy
y = copier(x, memo)
File "C:\Python26\lib\copy.py", line 255, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Python26\lib\copy.py", line 189, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\Python26\lib\copy.py", line 323, in _reconstruct
y = callable(*args)
File "C:\Python26\lib\copy_reg.py", line 93, in __newobj__
return cls.__new__(cls, *args)
TypeError: object.__new__(generator) is not safe, use generator.__new__()
>>>
Run Code Online (Sandbox Code Playgroud)
包含对第338,162,255,189行的引用的行在我复制的'第338行'之前重复了很多次.
你在克隆发电机吗?生成器无法克隆.
在这里复制Gabriel Genellina的答案:
没有办法"克隆"发电机:
py> g = (i for i in [1,2,3])
py> type(g)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create 'generator' instances
py> g.gi_code = code
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: readonly attribute
py> import copy
py> copy.copy(g)
Traceback (most recent call last):
...
TypeError: object.__new__(generator) is not safe, use generator.__new__()
py> type(g).__new__
<built-in method __new__ of type object at 0x1E1CA560>
Run Code Online (Sandbox Code Playgroud)
您可以使用生成器函数执行此操作,因为它充当"生成器
工厂",在调用时构建新生成器.即使使用Python C
API,创建一个生成器,也需要一个框架对象 - 并且无法
在运行中创建一个我知道的框架对象:(
py> import ctypes
py> PyGen_New = ctypes.pythonapi.PyGen_New
py> PyGen_New.argtypes = [ctypes.py_object]
py> PyGen_New.restype = ctypes.py_object
py> g = (i for i in [1,2,3])
py> g2 = PyGen_New(g.gi_frame)
py> g2.gi_code is g.gi_code
True
py> g2.gi_frame is g.gi_frame
True
py> g.next()
1
py> g2.next()
2
Run Code Online (Sandbox Code Playgroud)
g和g2共享相同的执行帧,因此它们不是独立的.有
没有简单的方法在Python中创建一个新的框架:
py> type(g.gi_frame)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create 'frame' instances
Run Code Online (Sandbox Code Playgroud)
人们可以尝试使用PyFrame_New - 但这对我来说太神奇了......