Skip to content

Python Prototype pattern is not correct #18

@lufton

Description

@lufton

I noticed here that there is __dict__.update() call. But in fact, this is overriding reference to previously shallow copied self.some_object_list.

    def __copy__(self):
        # First, let's create copies of the nested objects.
        some_list_of_objects = copy.copy(self.some_list_of_objects)
        some_circular_ref = copy.copy(self.some_circular_ref)

        # Then, let's clone the object itself, using the prepared clones of the nested objects.
        new = self.__class__(self.some_int, some_list_of_objects, some_circular_ref)
        
        new.__dict__.update(self.__dict__) # this looks wrong as it will override reference to copied list and object with references to original list and object
        # try adding this:
        assert self.some_list_of_objects is not new.some_list_of_objects, "It should reference different objects"
        assert self.some_circular_ref is not new.some_circular_ref, "It should reference different objects"

        return new

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions