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


相关推荐

  • 两个正序数组 找中位数_两个有序数组的中位数

    两个正序数组 找中位数_两个有序数组的中位数原题连接给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。示例 1:输入:nums1 = [1,3], nums2 = [2]输出:2.00000解释:合并数组 = [1,2,3] ,中位数 2示例 2:输入:nums1 = [1,2], nums2 = [3,4]输出:2.50000解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5示例 3:输入:nums1 = [0,

    2022年8月8日
    5
  • wireshark抓包分析IP数据报_fiddler抓包后怎么分析数据

    wireshark抓包分析IP数据报_fiddler抓包后怎么分析数据一.实验目的通过本次实验,掌握使用Wireshark抓取TCP/IP协议数据包的技能,能够深入分析IP帧格式。通过抓包和分析数据包来理解TCP/IP协议,进一步提高理论联系实践的能力。二.实验内容1.本次实验重点:利用Wireshark抓IP包及IP包的分析。2.本次实验难点:分析抓到的IP包。3.本次实验环境:Windows7,Wireshark。4.本次实验…

    2025年9月29日
    2
  • windows 10下无法安装.NET Framework 3.5

    windows 10下无法安装.NET Framework 3.5解决方案:win键+R,输入services.msc,“确定”打开服务,在右侧列表里找到“WindowsUpdate”双击打开后点击“启动”(若按钮灰色则把“启动类型”中的“禁用”改为“自动”)即可。(.NETFramework安装完成后如果你想继续关闭windowsupdate就继续把前面说的服务“停止”后“禁用”。)转载于:https://www.cnblogs…

    2022年6月1日
    52
  • 9_商品详情页面解决方案

    9_商品详情页面解决方案需求分析当搜索商品时,显示商品的详细信息,同时选择不同的sku,进行不同的数据显示解决方案商家更改数据微服务,通过消息队列MQ监听到发生变化,微服务调用者使用Thymeleaf模板,生成相应的静态页面,储存在本地磁盘,当用户发送请求到微服务时,使用nginx技术进行相应页面的返回商品详情页面静态化1、建Module:supergo_page2、改pom<?xmlversion=”1.0″encoding=”UTF-8″?><projectxmlns=”http

    2022年6月25日
    26
  • Java实现冒泡排序(详解)[通俗易懂]

    Java实现冒泡排序(详解)[通俗易懂]深度解析冒泡排序算法publicclassMySort{publicstaticvoidbubbleSort(intarray[]){for(inti=0;i<array.length;i++){for(intj=0;j<array.length-1-i;j++){if(array[j]>array[j+1]){

    2022年6月21日
    23
  • java integer最大值_java int型最大值/最小值,最大值+1,最小值-1

    java integer最大值_java int型最大值/最小值,最大值+1,最小值-1java中,int型变量是有符号整形变量。int型变量占用4个字节(32bit位)。int型变量采用补码形式来表示数值。对于一个二进制数,正数的补码是其本身,负数的补码是所有二进制位取反再加一。int变量中,第一位是符号位(0表示正数,1表示负数)。我们下面来实际分析int型中正数和负数是怎么表示的。5数字为正数,第一位符号为是0,正数5的二进制码是000000000000101,补码还是…

    2025年10月5日
    2

发表回复

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

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