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


相关推荐

  • Pycharm+Django之Django学习(1)(初学者)

    Pycharm+Django之Django学习(1)(初学者)教学属于博主的自学记录,适合刚学习Django的同学,如果比较熟的就不用look了!!!以下都是讲在windows上的部署情况;准备:1、Python+pycharm(下面是博主使用的版本,可自行安装)链接:https://pan.baidu.com/s/1th08XXTqf30Oh0-s7QgL1w密码:r6tc2、安装Django(可以到官网下载,也可使用Python自带…

    2025年9月2日
    5
  • 手机端自适应布局(自适应布局如何实现)

    移动前端自适应解决方案和比较

    2022年4月16日
    142
  • Auto Layout 使用心得(六)—— 制造炫酷的下拉刷新动画

    Auto Layout 使用心得(六)—— 制造炫酷的下拉刷新动画

    2021年9月6日
    53
  • Windows Server 2012 域控服务器连接转换服务器实现office服务功能

    Windows Server 2012 域控服务器连接转换服务器实现office服务功能nbsp 由于需求点上需要接入 office 相关功能 所以学习了搭建 windowsserve 相关知识点 以下是从 0 到有的一个过程配置信息 WindowsServe 版本 windows server 2012 OfficeOnline 版本 OfficeOnline 一 需要 2 台 windowsserve 的虚拟机 镜像的话这边上

    2025年10月25日
    4
  • 运算放大器典型电路及原理

    运算放大器典型电路及原理1.运算放大器工作原理综述:  运算放大器组成的电路五花八门,令人眼花瞭乱,在分析运算放大器工作原理时倘没有抓住核心,往往令人头大。本文收集运放电路的应用电路,希望看完后有所收获。但是在分析各个电路之前,还是先回忆一下两个运放教材里必教的技能,就是“虚短”和“虚断”。“虚短”是指在分析运算放大器处于线性状态时,可把两输入端视为等电位,这一特性称为虚假短路,简称虚短。显然不能将两输入端真正短路。…

    2022年4月29日
    54
  • Vue 后台管理系统 Demo

    Vue 后台管理系统 DemoGITHUB 原文地址 https github com lin xin vue manage system 前言该方案作为一套多功能的后台框架模板 适用于绝大部分的后台管理系统 WebManagemen 开发 基于 vue js 使用 vue cli3 脚手架 引用 ElementUI 组件库 方便开发快速简洁好看的组件 分离颜色样式 支持手动切换主题色 而且很方便

    2026年2月1日
    0

发表回复

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

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