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.
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.
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?
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.
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.
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.
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.
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]
All python variables are bindings to some objects in memory.
- Does this code do anything?
Yes, it does. Actually it creates a new object in memory with new name.
- How does
copy.copy(node
) differ fromnode = 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With