Why there is no difference between shallow copy and deep copy for a list of immutables

darxtrix

Suppose i have a python list l consisting of immutables.When i am doing a shallow copy and a deep copy, the result is same:

>>> a = (1,2) # immutable types
>>> b = (3,4)
>>> l = [a,b] # a list containing immutable types

>>> import copy
>>> y = copy.copy(l) # shallow copy
>>> z = copy.deepcopy(l) # deep copy

>>> id(l[0])
139857440375584
>>> id(y[0])
139857440375584
>>> id(z[0])
139857440375584 # all have the same id's , so all refer to the same object

Does it means that shallow copy and deep copy has a meaning only for collections of mutable types because immutable types never changes. But if we want to achieve a deep copy like behavior for the these type of collections how can we do that?

Regards.

thefourtheye

Since you cannot change the immutable objects, there is no point in creating copies of the same while copying.

Shallow Copy

As per copy's source code, shallow copy of immutable types is done like this

def _copy_immutable(x):
    return x

for t in (type(None), int, long, float, bool, str, tuple,
          frozenset, type, xrange, types.ClassType,
          types.BuiltinFunctionType, type(Ellipsis),
          types.FunctionType, weakref.ref):
    d[t] = _copy_immutable

For all the immutable types, the _copy_immutable function returns the object as it is, during shallow copy.

Deep Copy

The same way, during the deepcopy of tuples, the object is returned as is, as per the _deepcopy_tuple function,

d = id(x)
try:
    return memo[d]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to do Junit for shallow/deep copy of LinkedList

分類Dev

shallow copy in python

分類Dev

How to deep copy duplicate a list of dictionaries?

分類Dev

Deep Copy Constructor in Java

分類Dev

Make a deep copy of a dictionary

分類Dev

Shallow copy JavaScript object without references

分類Dev

How to do "Deep Copy" in Swift?

分類Dev

Copy values between workbooks

分類Dev

Clone/Copy unmodifable list

分類Dev

System.arraycopy() shallow copy or deepcopy with primitive and object references

分類Dev

Python Pandas DataFrame copy(deep = False)vs copy(deep = True)vs '='

分類Dev

How to copy blobs between containers?

分類Dev

Python: How to copy a list of a dictionaries

分類Dev

Why Doesn't reinterpret_cast Force copy_n for Casts between Same-Sized Types?

分類Dev

Difference between bytearray and list

分類Dev

Why does an initializer list cause 2 copies of data and not just the a single copy to pass it to the function?

分類Dev

How to copy list values to another list in flutter

分類Dev

Deep copying structs with char arrays in C (How to copy the arrays?)

分類Dev

If var seems to deep copy arrays in Swift. Does if let?

分類Dev

Deep copy of a Javascript object is not working as expected in Vue.js

分類Dev

copy initialization : why move or copy constructor was not called even if copy-elision is turned off?

分類Dev

How to copy data between two vectors faster?

分類Dev

Is it possible to copy a bootable VHD between computers?

分類Dev

Copy only files between two times

分類Dev

Apache camel copy file between directories

分類Dev

Why is k9copy no longer in use?

分類Dev

Why does copy assignment operator return *this?

分類Dev

Why does size increase after I copy?

分類Dev

Why does cp not copy a specified folder

Related 関連記事

  1. 1

    how to do Junit for shallow/deep copy of LinkedList

  2. 2

    shallow copy in python

  3. 3

    How to deep copy duplicate a list of dictionaries?

  4. 4

    Deep Copy Constructor in Java

  5. 5

    Make a deep copy of a dictionary

  6. 6

    Shallow copy JavaScript object without references

  7. 7

    How to do "Deep Copy" in Swift?

  8. 8

    Copy values between workbooks

  9. 9

    Clone/Copy unmodifable list

  10. 10

    System.arraycopy() shallow copy or deepcopy with primitive and object references

  11. 11

    Python Pandas DataFrame copy(deep = False)vs copy(deep = True)vs '='

  12. 12

    How to copy blobs between containers?

  13. 13

    Python: How to copy a list of a dictionaries

  14. 14

    Why Doesn't reinterpret_cast Force copy_n for Casts between Same-Sized Types?

  15. 15

    Difference between bytearray and list

  16. 16

    Why does an initializer list cause 2 copies of data and not just the a single copy to pass it to the function?

  17. 17

    How to copy list values to another list in flutter

  18. 18

    Deep copying structs with char arrays in C (How to copy the arrays?)

  19. 19

    If var seems to deep copy arrays in Swift. Does if let?

  20. 20

    Deep copy of a Javascript object is not working as expected in Vue.js

  21. 21

    copy initialization : why move or copy constructor was not called even if copy-elision is turned off?

  22. 22

    How to copy data between two vectors faster?

  23. 23

    Is it possible to copy a bootable VHD between computers?

  24. 24

    Copy only files between two times

  25. 25

    Apache camel copy file between directories

  26. 26

    Why is k9copy no longer in use?

  27. 27

    Why does copy assignment operator return *this?

  28. 28

    Why does size increase after I copy?

  29. 29

    Why does cp not copy a specified folder

ホットタグ

アーカイブ