DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String shallow & deep copy in Python

Buy Me a Coffee

*Memo for shallow and deep copy:

*Memo for others:


The same string is referred to, not shallow-copied and deep-copied.


A string is experimented as 2D, doing an assignment and shallow and deep copy as shown below:

*Memo:

  • A 2D string cannot be shallow-copied and deep-copied.
  • A string has infinite dimensions(D).
  • There are an assignment and 2 kinds of copies, shallow copy and deep copy:
    • An assignment is to create the one or more references to the original top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A shallow copy is to create the one or more references to the new top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A deep copy is to create the two or more references to the new top level object and the new lower levels' objects which you desire but at least the new 2nd level objects, keeping the same values as before:
      • A deep copy is the multiple recursions of a shallow copy so a deep copy can be done with multiple shallow copies.
    • Basically, immutable(hashable) objects aren't copied to save memory like str, bytes, int, float, complex, bool and tuple.

<Assignment>:

*Memo:

  • v1 and v2 refer to the same string and each same character.
  • is keyword can check if v1 and v2 refer to the same string and each same character.

A 2D string is assigned to a variable without copied as shown below:

   # String
#    ↓↓↓↓↓↓↓ 
v1 = 'ABCDE'
    # ↑↑↑↑↑ Each character
v2 = v1

print(v1, v1[2]) # ABCDE C
print(v2, v2[2]) # ABCDE C

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

<Shallow copy>:

*Memo:

  • v1 and v2 refer to the same string and each same character.

copy.copy() cannot shallow-copy a 2D string as shown below:

import copy

v1 = 'ABCDE'
v2 = copy.copy(v1)

print(v1, v1[2]) # ABCDE C
print(v2, v2[2]) # ABCDE C

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

str() cannot shallow-copy a 2D string as shown below:

v1 = 'ABCDE'
v2 = str(v1)

print(v1, v1[2]) # ABCDE C
print(v2, v2[2]) # ABCDE C

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

Slicing cannot shallow-copy the 2D string as shown below:

v1 = 'ABCDE'
v2 = v1[:]

print(v1, v1[2]) # ABCDE C
print(v2, v2[2]) # ABCDE C

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • v1 and v2 refer to the same string and each same character.

copy.deepcopy() cannot deep-copy and even shallow-copy a 2D string as shown below:

import copy

v1 = 'ABCDE'
v2 = copy.deepcopy(v1)

print(v1, v1[2]) # ABCDE C
print(v2, v2[2]) # ABCDE C

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

Additionally, copy.deepcopy() cannot deep-copy and even shallow-copy a string as 3D as shown below:

import copy

v1 = 'ABCDE'
v2 = copy.deepcopy(v1)

print(v1, v1[2], v1[2][0]) # ABCDE C C
print(v2, v2[2], v2[2][0]) # ABCDE C C

print(v1 is v2, v1[2] is v2[2], v1[2][0] is v2[2][0])
# True True True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)