大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
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__()
>>>
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.
Copying answer by Gabriel Genellina here:
There is no way of “cloning” a generator:
py> g = (i for i in [1,2,3])
py> type(g)()
Traceback (most recent call last):
File “”, line 1, in
TypeError: cannot create ‘generator’ instances
py> g.gi_code = code
Traceback (most recent call last):
File “”, line 1, in
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__
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 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 “”, line 1, in
TypeError: cannot create ‘frame’ instances
One could try using PyFrame_New — but that’s way too magic for my taste…
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/188398.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...