"awq/vscode:/vscode.git/clone" did not exist on "724bda5884aba6a6d9d049f474aeeefb8b1ad070"
test_copy_weak_ref.py 1.71 KB
Newer Older
zzg_666's avatar
zzg_666 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import copy
import weakref

class NestedObj:
    def __init__(self, value):
        self.value = value

df = NestedObj(4)
class MyObj:
    def __init__(self):
        self.my_list_int = [1, 2, 3]
        self.my_list_obj = [NestedObj(1), NestedObj(2), NestedObj(3)]
        self.reference_attr = weakref.ref(df)  # Weak reference

    def __deepcopy__(self, memo):
        # Create a new object
        new_obj = MyObj()
        
        # Deep copy all attributes
        new_obj.my_list_int = copy.deepcopy(self.my_list_int, memo)
        new_obj.my_list_obj = copy.deepcopy(self.my_list_obj, memo)
        
        # Copy the weak reference directly
        new_obj.reference_attr = self.reference_attr
        
        return new_obj

def test_custom_deepcopy():
    # 创建对象 a
    a = MyObj()
    
    # 深拷贝对象 a 到对象 b
    b = copy.deepcopy(a)
    
    # 修改 b 的 list 和 weak reference
    b.my_list_int.append(4)
    b.my_list_obj[0].value = 100
    b.reference_attr().value = 200
    
    # 检查 a 的 list 和 reference_attr 是否发生改变
    print("Original int list:", a.my_list_int)
    print("Copied int list:", b.my_list_int)
    
    print("Original obj list:", [obj.value for obj in a.my_list_obj])
    print("Copied obj list:", [obj.value for obj in b.my_list_obj])
    
    print("Original reference_attr value:", a.reference_attr().value)
    print("Copied reference_attr value:", b.reference_attr().value)

    b.reference_attr().value = None  # Clear the weak reference in b
    print("After clearing, original reference_attr value:", a.reference_attr().value)
    print("After clearing, copied reference_attr value:", b.reference_attr().value)

if __name__ == "__main__":
    test_custom_deepcopy()