我正在处理一些类,使用pygraph模块,当我使用add_node()方法时,它总是出现“node已经在图中”。因此,我尝试使用深度复制()来创建一个新实例,并遇到一些问题:
class test:
_storage = []
def add_item(self,item):
self._storage.append(item)
def pop_item(self,item):
return self._storage.pop()
def __repr__(self):
return '%s' %self._storage[:]
if __name__ == '__main__':
a1 = test()
a1.add_item(3)
a1.add_item(4)
from copy import copy,deepcopy
a2 = copy(a1)
a3 = deepcopy(a2)它显示:
In[28]: a1
Out[28]: [3, 4]
In[29]: a2
Out[29]: [3, 4]
In[30]: a3
Out[30]: [3, 4]但是,如果我在列表a2中添加了一些新项,a3也会发生变化
In[31]: a1.add_item(440)
In[32]: a2
Out[32]: [3, 4, 440]
In[33]: a3
Out[33]: [3, 4, 440]那么,我如何才能复制一些实例,并且它的变量与旧实例中的变量是独立的呢?
发布于 2014-01-17 03:10:18
_storage是一个类变量。deepcopy不会生成整个类的新副本,因此副本仍然会共享相同的类变量。您需要的是一个实例变量:
def __init__(self):
self._storage = []发布于 2014-01-17 03:29:04
class test:
storage = []
def add_item(self,item):
self.storage.append(item)
def pop_item(self,item):
return self.storage.pop()
def __repr__(self):
return '%s' %self.storage[:]
def __copy__(self):
print "COPY!!!" #define explicit copy behaviour
s = self.__class__()
s.storage = self.storage[:] # make a copy of the list rather than a pointer to same
return s
from copy import copy
a = test()
a.add_item(3)
a.add_item(4)
b = copy(a) #this will call the __copy__ method of the class
b.add_item(244)
a.add_item(5)
print a,b或者,您只需在类上创建一个copy方法并调用b = a.copy()
或者您可以使用user2357112的解决方案,它确实有效(如果您实际上不需要定义显式行为的话,那么它就会更像琵琶)。
>>> class test:
... def __init__(self):
... self.storage = []
... def add_item(self,item):
... self.storage.append(item)
... def pop_item(self,item):
... return self.storage.pop()
... def __repr__(self):
... return '%s' %self.storage[:]
...
>>> a = test()
>>> a.add_item(1)
>>> b = deepcopy(a)
>>> b.add_item(4)
>>> b
[1, 4]
>>> a
[1]https://stackoverflow.com/questions/21177109
复制相似问题