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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机基础三: 二进制减法实现[通俗易懂]

    计算机基础三: 二进制减法实现[通俗易懂]在上一章中了解了如何实现二进制加法,加法是始终从两个加数的最右列向左列进位计算的,而在减法中没有进位,只有借位.253-176=77上面的式子我们不难算出来,但习惯性的思维让我们用借位的方式求值.在不借位的情况下如何实现计算?借位是很麻烦的事情,虽然我们能够实现它,但这意味着额外的开销.我们将用一个小技巧,让我们避开借位从而实现减法.为了避免借位,我们先从百位最大值999中减去减数,而非从原来的被减数中减去减数.999-176=823这个方法称为对9求补

    2022年9月24日
    4
  • Idea激活码最新教程2023.1.1版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2023.1.1版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2023 1 1 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2023 1 1 成功激活

    2025年5月26日
    2
  • goland激活码 2021_通用破解码

    goland激活码 2021_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    479
  • 如何下载pycharm以前的版本_pycharm怎么下载包

    如何下载pycharm以前的版本_pycharm怎么下载包地址:https://www.jetbrains.com/pycharm/download/previous.html点击进去就能看到各个版本的下载链接。————————————————版权声明:本文为CSDN博主「lyj156」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/lyj156/article/details/95887590…

    2022年8月25日
    5
  • AutoDL算力租用++Pycharm中SSH、SFTP连接远程服务器[通俗易懂]

    AutoDL算力租用++Pycharm中SSH、SFTP连接远程服务器[通俗易懂]本文主要涉及GPU租用以及Pycharm中SSH、SFTP连接远程服务器

    2022年6月24日
    34
  • 最棒的java代码生成器「建议收藏」

    最棒的java代码生成器「建议收藏」一款Java语言基于SpringBoot2.x、Layui、Thymeleaf、MybatisPlus、Shiro、MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架,可用于快速搭建前后端分离后台管理系统,本着简化开发、提升开发效率的初衷,自研了一套个性化的组件,实现了可插拔的组件式开发方式:单图上传、多图上传、下拉选择、开关按钮、单选按钮、多选按钮、图片裁剪等

    2022年5月1日
    79

发表回复

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

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