3

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.

Ulises CT
  • 1,361
  • 1
  • 12
  • 21
  • 1
    Could you paste one or two examples of such objects ? More, did you read [this](http://stackoverflow.com/questions/184643/what-is-the-best-way-to-copy-a-list) and [this](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) ? –  Nov 29 '16 at 08:33
  • If you need to save original objects with all their attributes unchanged use `copy.deepcopy`. – Gennady Kandaurov Nov 29 '16 at 08:37
  • maybe you can write a copy-method, which just copys the attributes with copy.deepcopy and leaves the long arrays unchanged, of they are not needed as a copy – Markus Dutschke Nov 29 '16 at 09:01
  • @GennadyKandaurov what's the difference with copy.copy? and what about performance which is the main question? – Ulises CT Nov 29 '16 at 09:02
  • `copy.copy` is a shallow copy, better to read https://docs.python.org/2/library/copy.html. `deepcopy` has much worst performance since it has to recursively copy all attributes of the object. – Gennady Kandaurov Nov 29 '16 at 09:10
  • Ok then I guess i'll use copy.copy since it's working for what I need. You can add that questions as an answer so I choose it as answer @GennadyKandaurov – Ulises CT Nov 29 '16 at 09:20

1 Answers1

0

copy.copy creates a shallow copy of an object, which has a good description here.

copy.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.

gmauch
  • 1,316
  • 4
  • 25
  • 39
Gennady Kandaurov
  • 1,914
  • 1
  • 15
  • 19