0
Fixed

Prototype Python example error

Mingxuan Hu 2 months ago updated by Alexander Shvets 1 month ago 3

Problem

Here is the quote from the third and fourth lines of the output.txt:

Adding elements to `deep_copied_component`'s some_list_of_objects adds it to `component`'s some_list_of_objects.
Changing objects in the `component`'s some_list_of_objects changes that object in `deep_copied_component`'s some_list_of_objects.

Simply put, it says that adding elements to deep-copied objects affects the original objects and vice versa. However, this sounds buggy, as far as I understand, deep-copied objects and the original objects are independent.

A Possible Solution

1. change "new.__dict__.update(self.__dict__)"    (line 67, in SomeComponent.__deepcopy__)

    to        "new.__dict__ = copy.deepcopy(self.__dict__, memo)"

    reson: the origianl code may overwirte the deep-copyed filed some_list_of_objects.

2. change "another object" (line 116 and 117)

    to "another object2"

    reason:  element "another object" has been added when testing shallow_copied_component.

Result

After these two modifications, the output looks more reasonable (to me)

Adding elements to `deep_copied_component`'s some_list_of_objects doesn't add it to `component`'s some_list_of_objects.
Changing objects in the `component`'s some_list_of_objects doesn't change that object in `deep_copied_component`'s some_list_of_objects.

P.S.

However, I am new to python and all of these may be nonsense. If this is the case, would anyone be kind enough to tell me why :)