Evg*_*ich 3 python deep-copy cyclic-reference
我有一个具有一类__eq__和__hash__重写,使其对象作为字典键.每个对象还带有一个字典,由同一类的其他对象键入.AttributeError当我尝试deepcopy整个结构时,我感到很奇怪.我在OsX上使用Python 3.6.0.
从Python文档看起来好像deepcopy使用memo字典来缓存它已经复制的对象,所以嵌套结构应该不是问题.那我做错了什么?我应该编写自己的__deepcopy__方法来解决这个问题吗?怎么样?
from copy import deepcopy
class Node:
def __init__(self, p_id):
self.id = p_id
self.edge_dict = {}
self.degree = 0
def __eq__(self, other):
return self.id == other.id
def __hash__(self):
return hash(self.id)
def add_edge(self, p_node, p_data):
if p_node not in self.edge_dict:
self.edge_dict[p_node] = p_data
self.degree += 1
return True
else:
return False
if __name__ == '__main__':
node1 = Node(1)
node2 = Node(2)
node1.add_edge(node2, "1->2")
node2.add_edge(node1, "2->1")
node1_copy = deepcopy(node1)
Run Code Online (Sandbox Code Playgroud)
File ".../node_test.py", line 15, in __hash__
return hash(self.id)
AttributeError: 'Node' object has no attribute 'id'
Run Code Online (Sandbox Code Playgroud)
循环依赖是一个问题,deepcopy当你:
问题是取消对象(deepcopy默认情况下,通过pickling和unpickling复制自定义对象,除非__deepcopy__定义了特殊方法)创建空对象而不初始化它,然后尝试逐个填充其属性.当它试图填写node1的属性时,它需要初始化node2,而后者又依赖于部分创建node1(在两种情况下都是由于edge_dict).当它试图填写edge_dict一个时Node,Node它的添加尚未设置edge_dict其id属性,因此散列它的尝试失败.
您可以通过使用__new__来确保在初始化可变,可能是递归属性之前建立不变量,并定义pickle帮助程序__getnewargs__(或__getnewargs_ex__)以使其正确使用它们来纠正此问题.具体来说,将您的类定义更改为:
class Node:
# __new__ instead of __init__ to establish necessary id invariant
# You could use both __new__ and __init__, but that's usually more complicated
# than you really need
def __new__(cls, p_id):
self = super().__new__(cls) # Must explicitly create the new object
# Aside from explicit construction and return, rest of __new__
# is same as __init__
self.id = p_id
self.edge_dict = {}
self.degree = 0
return self # __new__ returns the new object
def __getnewargs__(self):
# Return the arguments that *must* be passed to __new__
return (self.id,)
# ... rest of class is unchanged ...
Run Code Online (Sandbox Code Playgroud)
注意:如果这是Python的2码,确保从明确地继承object和改变super(),以super(Node, cls)中__new__; 给出的代码是更简单的Python 3代码.
另一种解决方案只能 处理copy.deepcopy,不支持酸洗或需要使用__new__/ __getnewargs__(需要新式类),只能覆盖深度复制.您将在原始类上定义以下额外方法(并确保模块导入copy),否则保持不变:
def __deepcopy__(self, memo):
# Deepcopy only the id attribute, then construct the new instance and map
# the id() of the existing copy to the new instance in the memo dictionary
memo[id(self)] = newself = self.__class__(copy.deepcopy(self.id, memo))
# Now that memo is populated with a hashable instance, copy the other attributes:
newself.degree = copy.deepcopy(self.degree, memo)
# Safe to deepcopy edge_dict now, because backreferences to self will
# be remapped to newself automatically
newself.edge_dict = copy.deepcopy(self.edge_dict, memo)
return newself
Run Code Online (Sandbox Code Playgroud)