Skip to content

Commit 868a3a4

Browse files
committed
Content fixes.
1 parent f1b4742 commit 868a3a4

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/Prototype/Conceptual/Output.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Adding elements to `shallow_copied_component`'s some_list_of_objects doesn't add it to `component`'s some_list_of_objects.
1+
Adding elements to `shallow_copied_component`'s some_list_of_objects adds it to `component`'s some_list_of_objects.
22
Changing objects in the `component`'s some_list_of_objects changes that object in `shallow_copied_component`'s some_list_of_objects.
3-
Adding elements to `deep_copied_component`'s some_list_of_objects doesn't add it to `component`'s some_list_of_objects.
4-
Changing objects in the `component`'s some_list_of_objects doesn't change that object in `deep_copied_component`'s some_list_of_objects.
5-
id(deep_copied_component.some_circular_ref.parent): {id(deep_copied_component.some_circular_ref.parent)}
6-
id(deep_copied_component.some_circular_ref.parent.some_circular_ref.parent): {id(deep_copied_component.some_circular_ref.parent.some_circular_ref.parent)}
7-
^^ This shows that deepcopied objects contain same reference, they are not cloned repeatedly.
3+
Adding elements to `deep_copied_component`'s some_list_of_objects adds it to `component`'s some_list_of_objects.
4+
Changing objects in the `component`'s some_list_of_objects changes that object in `deep_copied_component`'s some_list_of_objects.
5+
id(deep_copied_component.some_circular_ref.parent): 4429472784
6+
id(deep_copied_component.some_circular_ref.parent.some_circular_ref.parent): 4429472784
7+
^^ This shows that deepcopied objects contain same reference, they are not cloned repeatedly.

src/Prototype/Conceptual/main.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ def __copy__(self):
2828
`copy.copy` with this object and the returned value is returned as the
2929
new shallow copy.
3030
"""
31+
32+
# First, let's create copies of the nested objects.
33+
some_list_of_objects = copy.copy(self.some_list_of_objects)
34+
some_circular_ref = copy.copy(self.some_circular_ref)
35+
36+
# Then, let's clone the object itself, using the prepared clones of the
37+
# nested objects.
3138
new = self.__class__(
32-
self.some_int, self.some_list_of_objects, self.some_circular_ref
39+
self.some_int, some_list_of_objects, some_circular_ref
3340
)
3441
new.__dict__.update(self.__dict__)
3542

36-
# The new object has a new list of objects but with same
37-
# objects(shared).
38-
new.some_list_of_objects = copy.copy(self.some_list_of_objects)
39-
new.some_circular_ref = copy.copy(self.some_circular_ref)
4043
return new
4144

4245
def __deepcopy__(self, memo={}):
@@ -51,15 +54,18 @@ def __deepcopy__(self, memo={}):
5154
to all the `deepcopy` calls you make in the `__deepcopy__` implementation
5255
to prevent infinite recursions.
5356
"""
57+
58+
# First, let's create copies of the nested objects.
59+
some_list_of_objects = copy.deepcopy(self.some_list_of_objects, memo)
60+
some_circular_ref = copy.deepcopy(self.some_circular_ref, memo)
61+
62+
# Then, let's clone the object itself, using the prepared clones of the
63+
# nested objects.
5464
new = self.__class__(
55-
self.some_int, self.some_list_of_objects, self.some_circular_ref
65+
self.some_int, some_list_of_objects, some_circular_ref
5666
)
5767
new.__dict__.update(self.__dict__)
5868

59-
# The new object has a new list of objects with different copy of those
60-
# objects.
61-
new.some_list_of_objects = copy.deepcopy(self.some_list_of_objects, memo)
62-
new.some_circular_ref = copy.deepcopy(self.some_circular_ref, memo)
6369
return new
6470

6571

@@ -138,11 +144,11 @@ def __deepcopy__(self, memo={}):
138144

139145
print(
140146
f"id(deep_copied_component.some_circular_ref.parent): "
141-
"{id(deep_copied_component.some_circular_ref.parent)}"
147+
f"{id(deep_copied_component.some_circular_ref.parent)}"
142148
)
143149
print(
144150
f"id(deep_copied_component.some_circular_ref.parent.some_circular_ref.parent): "
145-
"{id(deep_copied_component.some_circular_ref.parent.some_circular_ref.parent)}"
151+
f"{id(deep_copied_component.some_circular_ref.parent.some_circular_ref.parent)}"
146152
)
147153
print(
148154
"^^ This shows that deepcopied objects contain same reference, they "

0 commit comments

Comments
 (0)