I have defined a Color class as below. Since I need to store multiple colors and their respective node ID (which has the color), I made a list colors to store them. However, every time a node color is changed, I do not want to update list colors directly (another function will decide whether to update or not), so I need to store a copy of colors to *tmp_colors* before calling the decision func, and update colors with *tmp_colors* if result is Yes.
I managed to make a copy of new list *tmp_colors*, but *tmp_colors[0]* is still pointing to colors[0] , resulting in the update of both list.
- How can I make a copy of class object in colors[0] to *tmp_colors[0]*?
- If I were to update colors[0] later on, what's the best way?
- Is there any better design instead of the example below (define class, and list of class object)?
class Color:
__elems__ = "num", "nodelist",
def __init__(self):
self.num = 0
self.num_bad_edge = 0
def items(self):
return [
(field_name, getattr(self, field_name))
for field_name in self.__elems__]
def funcA():
nodeCount = 2
colors = []
for i in range(0, nodeCount):
colors.append(Color())
colors[0].num = 2
colors[0].nodelist = [10,20]
colors[1].num = 3
colors[1].nodelist = [23,33, 43]
print "colors"
for i in range(0, nodeCount):
print colors[i].items()
tmp_colors = list(colors)
print "addr of colors:"
print id(colors)
print "addr of tmp_colors:"
print id(tmp_colors)
print "addr of colors[0]:"
print id(colors[0])
print "addr of tmp_colors[0]:"
print id(tmp_colors[0])
tmp_colors[0].num = 2
tmp_colors[0].nodelist = [10,21]
print "\ntmp_colors"
for i in range(0, nodeCount):
print tmp_colors[i].items()
print "\ncolors <<< have been changed"
for i in range(0, nodeCount):
print colors[i].items()
Result:
colors
[('num', 2), ('nodelist', [10, 20])]
[('num', 3), ('nodelist', [23, 33, 43])]
addr of colors:
32480840
addr of tmp_colors:
31921032
addr of colors[0]:
32582728
addr of tmp_colors[0]:
32582728 <<<<<< --- expecting a new addr
tmp_colors
[('num', 2), ('nodelist', [10, 21])]
[('num', 3), ('nodelist', [23, 33, 43])]
colors <<< have been changed
[('num', 2), ('nodelist', [10, 21])] <<<<<< --- expecting no change [10, 20]
[('num', 3), ('nodelist', [23, 33, 43])]