- Intent:
- Copying other’s object without making your code dependene on it. Clone existing objects as prototypes
- Problem
- Creating an exact copy of an object from the outside may not be possible due to private encapsulation
- Copying can be very expensive. Use when creating a new instance is cheaper
- Flow
- Client receives object. Calls
clone(deep: bool = True)
on it, accepting a param defining if its a shallow or deep copy
- Solution
- Create a shallow copy ⇒ for nested objects it provides the same reference (i.e., a pointer) to the nested objects. This will cause for mutable objects to be changed and the copy be affected.
- Create a deep copy ⇒ recursively copies all nested objects included
- Copy
- Use cases
- Loading data from a database
- Complex calculations