使用深拷贝实现递归
如何在深拷贝函数对象中实现递归?这是相关代码(如果您想要更多,请询问): PS:我希望递归能够迭代经过过滤的引用列表。目标是下载并插入任何丢失的对象。
复制.py
from put import putter
class copier:
def __init__(self, base):
self.base = base
def copyto(self, obj):
put = putter(obj)
for x in self.base.__dict__:
put(x)
放置.py
class putter:
def __init__(self, parent):
self.parent = parent
def put(self, name, obj):
self.parent.__dict__[name] = obj
How can I implement recursion in a deep copy function object? This is the relevant code (if you want more then please ask):
PS: I would like the recursion to iterate through a filtered list of references. The goal is to download and insert any missing objects.
copy.py
from put import putter
class copier:
def __init__(self, base):
self.base = base
def copyto(self, obj):
put = putter(obj)
for x in self.base.__dict__:
put(x)
put.py
class putter:
def __init__(self, parent):
self.parent = parent
def put(self, name, obj):
self.parent.__dict__[name] = obj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
copy.deepcopy
的文档,看看您是否可以使用__getinitargs__()
、__getstate__()
和__setstate__ 实现您想要的功能()
,那么这将为您省去很多悲伤。否则,您需要自己重新实现它,它应该类似于:Check out the documentation for
copy.deepcopy
, if you can implement what you want with__getinitargs__()
,__getstate__()
and__setstate__()
, then that will save you a lot of grief. Otherwise, you will need to reimplement it yourself, it should look something like: