Python:copy.deepcopy产生错误 [英] Python: copy.deepcopy produces an error

查看:258
本文介绍了Python:copy.deepcopy产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在很多需要它的类中使用这种复制方法了很长时间了.

I have been using this copy method for quite a while, in lots of classes that needed it.

class population (list):
def __init__ (self):
    pass

def copy(self):
    return copy.deepcopy(self)

它突然开始产生此错误:

It has suddenly started producing this error:

     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__()
>>> 

包含对338、162、255、189行的引用的行在我在此处复制的"338行"之前重复了很多次.

the lines which include the references to lines 338, 162, 255, 189 were repeated quite a few times before the 'line 338' that I copied here.

推荐答案

您是否正在克隆生成器? 无法克隆生成器.

Are you cloning a generator? Generators can't be cloned.

在此处复制Gabriel Genellina的答案:

Copying answer by Gabriel Genellina here:

无法克隆"生成器:

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>

您可以使用生成器函数执行此操作,因为它可以充当生成器
工厂",则在调用时会构建一个新的生成器.甚至使用Python C
API,要创建一个生成器,就需要一个框架对象-而且没有办法
创建一个我所知道的:(

You can do that with a generator function because it acts as a "generator
factory", building a new generator when called. Even using the Python C
API, to create a generator one needs a frame object -- and there is no way
to create a frame object "on the fly" that I know of :(

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

g和g2共享相同的执行框架,因此它们不是独立的.那里
在Python中创建新框架绝非易事:

g and g2 share the same execution frame, so they're not independent. There
is no easy way to create a new frame in Python:

py> type(g.gi_frame)()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: cannot create 'frame' instances

一个人可以尝试使用PyFrame_New-但这对我来说太不可思议了...

One could try using PyFrame_New -- but that's way too magic for my taste...

这篇关于Python:copy.deepcopy产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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