编辑变量而不编辑原点

时间:2013-02-12 19:09:44

标签: reference python-2.7 mutable

我需要复制变量并对其进行更改。 我已经看过this了,但我希望相反。 在这段代码中,我需要返回的元组是两个不同的列表,不一样。

def getIPRange(self):
    group = [0, self.getJoinedNetworks() - 1]
    while True:
        if self.ip[2] > group[0] and self.ip[2] < group[1]:
            res_ip_min = self.ip      #self.ip is for example [70, 30, 20, 0]
            res_ip_min[2] = group[0]
            res_ip_min[3] = 1

            res_ip_max = self.ip
            res_ip_max[2] = group[1]
            res_ip_max[3] = 254

            return (res_ip_min, res_ip_max)

        else:
            group[0] = group[1] + 1
            group[1] = group[0] + self.getJoinedNetworks() - 1

1 个答案:

答案 0 :(得分:0)

最常见的可能是

...
res_ip_min = list(self.ip)
...
res_ip_max = list(self.ip)
...

从self.ip的元素创建一个新列表。或者,您可以使用python's copy utilities

相关问题