尝试更改列表中对象的值时出错

时间:2016-06-24 13:48:07

标签: python

存在:

绘图,分割对象

class Drawing(object):
    """docstring for Drawing"""
    def __init__(self, id, initial_x, initial_y, segments):
        super(Drawing, self).__init__()
        self.id = id
        self.segments = segments
        self.initial_x = initial_x
        self.initial_y = initial_y

class Segment(object):
    """docstring for Segment"""
    def __init__(self, p0, p1, type='LINE', attribs={}):
        super(Segment, self).__init__()
        self.p0 = p0
        self.p1 = p1
        self.type = type
        self.attribs = attribs

我想将attribs ['layer']的值更改为其他值但不会发生。

def _get_drawing_by_id(self, id):
    import copy
    for drawing in self.drawing_list:
        if drawing.id == id:
            d = drawing
            return d
    return None

function my_method(self):
    drawing = self._get_drawing_by_id(str(args[3]))
    for segment in drawing.segments:
        #print segment.attribs['layer'],
        layer = segment.attribs['layer']
        new_layer = self.swap_list[layer]
        segment.attribs['layer'] = new_layer
        print segment
        print segment.attribs

打印最后一个代码时,我得到:

<cadstar.parser.Segment object at 0x7fbe5177cb10>
{'layer': 'LAY21'}
<cadstar.parser.Segment object at 0x7fbe5177cb50>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cb90>
{'layer': 'LAY21'}
<cadstar.parser.Segment object at 0x7fbe5177cbd0>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cc10>
{'layer': 'LAY21'}
<cadstar.parser.Segment object at 0x7fbe5177cc50>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cc90>
{'layer': 'LAY21'}
<cadstar.parser.Segment object at 0x7fbe5177ccd0>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cd10>
{'layer': 'LAY21'}
<cadstar.parser.Segment object at 0x7fbe5177cd50>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cd90>
{'layer': 'LAY21'}
<cadstar.parser.Segment object at 0x7fbe5177cdd0>
{'layer': 'LAY20'}

但稍后我立即再次循环drawing.segments,我得到......

<cadstar.parser.Segment object at 0x7fbe5177cb10>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cb50>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cb90>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cbd0>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cc10>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cc50>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cc90>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177ccd0>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cd10>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cd50>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cd90>
{'layer': 'LAY20'}
<cadstar.parser.Segment object at 0x7fbe5177cdd0>
{'layer': 'LAY20'}

就像改变价值的过程之前一样。没有改变!!!!参考文献是一样的。

更新:

swap_list只是这种形式的字典{'LAY20':'LAY21','LAY18':'LAY19'}

0 个答案:

没有答案