Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is copy.copy different from assignment in python

Tags:

python

I have run across a piece of code that at first glance seems pointless. But I realize that this could have some unknown implications that I am not aware of as Python is not my most known language.

import copy
node = copy.copy(node)

Reading the documentation of copy it says that

copy.copy(x) Return a shallow copy of x.

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

  1. Does this code do anything? The node that it is copying is being taken from a scene list of objects. Is it creating a copy of just the root level of the class so it can change variables (name) but not effect the original in the list? The node class contains several node children.

  2. How does copy.copy(node) differ from node = node Is one constructing a new object while the other is simple pointing to the same object?

like image 579
marsh Avatar asked Mar 29 '16 13:03

marsh


People also ask

What is the difference between assignment and copy in Python?

The Copy constructor and the assignment operators are used to initialize one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.

Does assignment make a copy in Python?

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

What does Copy Copy do in Python?

It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In the case of deep copy, a copy of the object is copied into another object. It means that any changes made to a copy of the object do not reflect in the original object.

What is the difference between copy copy () and copy Deepcopy () in Python?

copy() create reference to original object. If you change copied object - you change the original object. . deepcopy() creates new object and does real copying of original object to new one. Changing new deepcopied object doesn't affect original object.


2 Answers

The assignment operator (=) only creates a reference to an object, and will create a new variable referencing the same memory address.

Copy will create a new object in memory, and then assign the variable to that.

For example:

import copy

node = [0, 1]
node2 = node
node3 = copy.copy(node)

node2.append(5)

print(node)
print(node2)
print(node3)

prints out:

[0, 1, 5]
[0, 1, 5]
[0, 1]
like image 176
timlyo Avatar answered Oct 24 '22 07:10

timlyo


All python variables are bindings to some objects in memory.

  1. Does this code do anything?

Yes, it does. Actually it creates a new object in memory with new name.

  1. How does copy.copy(node) differ from node = node?

By assignment you are getting different names for the same object. So if you are really need a copy you should use shallow or deep copy.

Example:

>>> x = [1]
>>> id(x)
35964488
>>> y = x
>>> id(y)
35964488
>>> z = x[:]
>>> id(z)
35964768

So x and y have same id, while z has different.

Same here:

>>> import copy
>>> c = copy.copy(x)
>>> id(c)
35985168
>>> id(x)
35964488

Quote from docs about shallow and deep copy:

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

like image 10
xiº Avatar answered Oct 24 '22 08:10

xiº