开发者

Variable referenced instead of copied

开发者 https://www.devze.com 2022-12-15 02:09 出处:网络
When I run the code below it removes deleted_partner from B. But as it removes it from B it also removes it from A. So that when I try to remove it from A the program crashes. What is the problem?

When I run the code below it removes deleted_partner from B. But as it removes it from B it also removes it from A. So that when I try to remove it from A the program crashes. What is the problem?

for deleted_partner in self.开发者_JS百科list_of_trading_partners: 
    B = A[:]
    print("t", deleted_partner)
    print(B[self.ID].list_of_trading_partners)
    B[self.ID].list_of_trading_partners.remove(deleted_partner)
    Round_neo_classic(B)
    Round_costs(B)
    if B[self.ID].final_good > reference_value:
        print("d", deleted_partner)
        print(A[self.ID].list_of_trading_partners)
        A[self.ID].list_of_trading_partners.remove(deleted_partner)

Output:

('t', 1)
[1, 2, 3, 4]
('d', 1)
[2, 3, 4]


You're not removing from B or A, but from A[some_ID].list_of_trading_partners and B[some_ID].list_of_trading_partners. [:] only makes a "shallow copy" of the list, in that it creates a new, separate list, but the elements contained in that list (one of which list_of_trading_partners is an attribute of) are not copied but referenced. Perhaps the copy module and its deepcopy function can help you?


B=A[:] does copy only the list, but not it's contents. B[self.ID] and A[self.ID] still reference the same object, only A and B are different.

You might way to explicitly copy all the elements of the list too - copy.deepcopy can do this. But beware: deepcopy copies everything - it looks like you only want to copy the list_of_trading_partners, so you should probably write a __deepcopy__ method on whatever class A[self.ID] is that does just that.


Since you do not put the real list is hard to do tests, buy you can try with copy.copy or copy.deepcopy

import copy

for deleted_partner in self.list_of_trading_partners: 
    B = copy.copy(A)
    print("t", deleted_partner)
    print(B[self.ID].list_of_trading_partners)
    B[self.ID].list_of_trading_partners.remove(deleted_partner)
    Round_neo_classic(B)
    Round_costs(B)
    if B[self.ID].final_good > reference_value:
        print("d", deleted_partner)
        print(A[self.ID].list_of_trading_partners)
        A[self.ID].list_of_trading_partners.remove(deleted_partner)
0

精彩评论

暂无评论...
验证码 换一张
取 消