copy.deepcopy()_python 内存管理

copy.deepcopy()_python 内存管理Ihavebeenusingthiscopymethodforquiteawhile,inlotsofclassesthatneededit.classpopulation(list):def__init__(self):passdefcopy(self):returncopy.deepcopy(self)Ithassuddenlystarted…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

copy.deepcopy()_python 内存管理

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…

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188398.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Latex常见符号对照表

    摘要:Latex可以很方便的利用命令来生成各式各样的特殊符号.这里根据官方的文档将这些常见符号列出,以备查用.

    2022年4月4日
    246
  • Flash Builder 4.5 导入一个新项目,提示“flex unable to open xxxxxxxxx.swc”

    Flash Builder 4.5 导入一个新项目,提示“flex unable to open xxxxxxxxx.swc”参考http://forums.adobe.com/thread/741783http://forums.esri.com/Thread.asp?c=158&f=2421&t=297291 1、如果提示FLEXSDKX.X找不到,就下载这个SDK,然后再WINDOWS/PREFERENCE里面吧SDK配置好路径2、提示“flexunabletoopenxxxxxxx

    2022年8月22日
    8
  • java面向对象三大特征及五大原则

    java面向对象三大特征及五大原则java面向对象一、java面向对象的三大特征1、封装(Encapsulation)封转是指属性私有化根据需要提供setter和getter方法来访问属性隐藏具体属性和实现细节,仅对外开放接口控制程序中属性的访问级别目的:增强数据安全性,不能让其他用户随意访问和修改数据,简化编程,使用者不必在意具体实现细节,而只是通过外部接口即可访问类的成员2、继承(Extend)继承是指将…

    2022年7月25日
    4
  • LM算法初识_lm算法效果

    LM算法初识_lm算法效果  由于工作内容接触到点云标定,需要用到最小二乘法,所以特意花了点时间研究LM算法,但是由于大学的高等数学忘得差不多了,所以本文从最基本的一些数学概念开始;信赖域法  在最优化算法中,都是要求一个函数的极小值,每一步迭代中,都要求目标函数值是下降的,而信赖域法,顾名思义,就是从初始点开始,先假设一个可以信赖的最大位移,然后在以当前点为中心,以为半径的区域内,通过寻找目标函数的一个近似函数(二次的)的最优点,来求解得到真正的位移。在得到了位移之后,再计算目标函数值,如果其使目标函数值的下降满足了一定条件,

    2022年10月1日
    0
  • pycharm每次新建项目都要重新安装一些第三方库解决办法

    pycharm每次新建项目都要重新安装一些第三方库解决办法目前有三个解决办法,也是亲测有用的:第一个方法:因为之前有通过pycharm的projectinterpreter里的+号添加过一些库,但添加的库只是指定的项目用的,如果想要用,就必须用之前的项目的python解释器,举个例子:这个是我之前的项目的解释器,这个项目解释器是继承的python的解释器,同时又安装了上面你看到的这些库,包含numpy和opencv-python等,然后我新…

    2022年5月17日
    35
  • 聊聊LuaJIT「建议收藏」

    聊聊LuaJIT「建议收藏」JIT什么是JITJIT=JustInTime即时编译,是动态编译的一种形式,是一种优化虚拟机运行的技术。程序运行通常有两种方式,一种是静态编译,一种是动态解释,即时编译混合了这二者。Ja

    2022年8月6日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号