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


相关推荐

  • 约瑟夫环——公式法(递推公式)

    约瑟夫环——公式法(递推公式)约瑟夫问题约瑟夫问题是个有名的问题:N个人围成一圈,第一个从1开始报数,报M的将被杀掉,下一个人接着从1开始报。如此反复,最后剩下一个,求最后的胜利者。例如只有三个个人,把他们叫做A、B、C围成一圈,从A开始报数,报2的人被杀掉。A开始报数,他报1。侥幸逃过一劫。然后轮到B报数,他报2。非常惨,他被杀了C接着从1开始报数然后轮到A报数,他报2。也被杀死了。最终胜利者是C解决方案普通解

    2022年5月4日
    47
  • 两种方式完成批处理的优缺点

    两种方式完成批处理的优缺点

    2021年10月3日
    52
  • pycharm pyinstaller打包exe_pip安装第三方库失败

    pycharm pyinstaller打包exe_pip安装第三方库失败1.安装时打开AnacondaPrompt,然后cdD:\Anaconda3\pkgs打开路径,输入安装命令:pipinstallPyInstaller。最后输入piplist查看2.调出terminal终端,输入命令例如pyinstaller-F-wvipvideoplay2.py点击回车如图:输入指定命令后会在当前目录下生产dist文件夹,dist文件夹下为生成的exe文件参数说明:-F:将所有库文件打包成一个exe-w:隐藏黑色控制台窗口如果不加-F参数会生成很多文

    2022年8月27日
    6
  • Google Doodle:龙年 新春快乐 龙年进步!

    Google Doodle:龙年 新春快乐 龙年进步!看过春晚,放过×××,Google终于在北京时间1月23日的凌晨放出了龙年的Doodle: 转载于:https://blog.51cto.com/maclean/1278817

    2022年7月26日
    2
  • Java学习之Hibernate框架使用

    Java学习之Hibernate框架使用0x00前言以我看来Hibernate的使用频率其实还是比较可观的,但都说Hibernate比较笨重,这里来学习一波,做个简单记录。0x01使用流程

    2021年12月13日
    40
  • adb命令 利用jks文件给apk签名[通俗易懂]

    adb命令 利用jks文件给apk签名[通俗易懂]程序猿日常实践是检验真理的唯一标准。jarsigner-verbose-keystorexxx.jks-signedjarxxx.apk(签名后的apk名字)xxx.apk(需要签名的apk)xxx(keystore别名)

    2022年5月30日
    34

发表回复

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

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