0

Possible error

yingfhu 3 тижні тому оновлено Alexander Shvets 3 тижні тому 0

https://refactoringguru.cn/design-patterns/prototype/python/example

In

SomeComponent.__copy__

the code here seems wrong, the intention is to copy the object but use `dict.update` in the end which replacing the cloned object. 

new.__dict__.update(self.__dict__)

No wonder the new and self share the same ids. Therefore the last line of `__copy__` and `__deepcopy__` should be removed. Instead, in client code, the outside list and nested list and set should be altered and check individually, such as 

# outside list not changed since copied
shallow_copied_component.some_list_of_objects.append("another object")
# inside nested list and set changed as well since shallow copied 
shallow_copied_component.some_list_of_objects[-1].append("another object")
shallow_copied_component.some_list_of_objects[1].add(4)