Ulises CT Ulises CT - 4 days ago 5
Python Question

Efficiency with copy.copy in Python

I need to copy some objects and I read that copy.copy module can do this on Python. The thing is, these objects have some attributes which are long arrays.

So is this method efficient? Since performance is important in this work I'm doing.

Is there a better way to do this?

I need this because I need to modify some attributes from the original object but then I need it without being modified also, something like an aux.

Cheers.

Answer

copy.copy creates a shallow copy of an object, which has a good description heredocs.python.org/2/library/copy.html. deepcopy has much worst performance since it has to recursively copy all attributes of the object, but deepcopy creates a real independent copy of an object.

Comments