Shallow copy & Deep copy in Python (6)


Buy Me a Coffee

*Memo:

  • My post explains the shallow and deep copy of a list.
  • My post explains the shallow and deep copy of a tuple.
  • My post explains the shallow and deep copy of the set with a frozenset.
  • My post explains the shallow and deep copy of the set with a tuple.
  • My post explains the shallow and deep copy of the set with an iterator.
  • My post explains the shallow and deep copy of a dictionary.
  • My post explains the shallow and deep copy of an iterator.
  • My post explains a frozenset (1).


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

*Memo:

  • For a 2D frozenset, only deep copy is possible.
  • frozenset.copy() doesn’t do shallow copy so I asked to remove it in the issue.
  • A frozenset can have the hashable types of elements like str, bytes, int, float, complex, bool, tuple, frozenset, range and iterator but cannot have the unhashable types of elements like bytearray, list, set and dict.



<Assignment>:

*Memo:

  • A and B refer to the same shallow and deep frozenset.
  • is keyword can check if A and B refer to the same frozenset.
    ##### Shallow frozenset #####
#   ↓↓↓↓↓↓↓↓↓↓↓                ↓↓ 
A = frozenset([frozenset(['a'])])
             # ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ Deep frozenset
B = A

print(A) # frozenset({frozenset({'a'})})
print(B) # frozenset({frozenset({'a'})})

print(A is B)
# True

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({'a'})
print(B) # frozenset({'a'})

print(A is B)
# True
Enter fullscreen mode

Exit fullscreen mode



<Shallow copy>:

frozenset.copy() doesn’t shallow-copy the 2D frozenset as shown below:

*Memo:

  • A and B refer to the same shallow and deep frozenset.
A = frozenset([frozenset(['a'])])
B = A.copy()

print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}

print(A is B)
# True

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({'a'})
print(B) # frozenset({'a'})

print(A is B)
# True
Enter fullscreen mode

Exit fullscreen mode

copy.copy() doesn’t shallow-copy a 2D frozenset as shown below:

import copy

A = frozenset([frozenset(['a'])])
B = copy.copy(A)

print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}

print(A is B)
# True

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({'a'})
print(B) # frozenset({'a'})

print(A is B)
# True
Enter fullscreen mode

Exit fullscreen mode

frozenset() doesn’t shallow-copy a 2D frozenset as shown below:

A = frozenset([frozenset(['a'])])
B = frozenset(A)

print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}

print(A is B)
# True

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({'a'})
print(B) # frozenset({'a'})

print(A is B)
# True
Enter fullscreen mode

Exit fullscreen mode



<Deep copy>:

copy.deepcopy() deep-copies a 2D frozenset as shown below:

*Memo:

  • A and B refer to different shallow and deep frozensets.
  • copy.deepcopy() should be used because it’s safe, copying deeply while frozenset.copy(), copy.copy() and frozenset() aren’t safe, copying shallowly.
import copy

A = frozenset([frozenset(['a'])])
B = copy.deepcopy(A)

print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}

print(A is B)
# False

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({'a'})
print(B) # frozenset({'a'})

print(A is B)
# False
Enter fullscreen mode

Exit fullscreen mode

Additionally, copy.deepcopy() deep-copies a 3D frozenset as shown below:

import copy

A = frozenset([frozenset([frozenset(['a'])])])
B = copy.deepcopy(A)

print(A) # frozenset({frozenset({frozenset({'a'})})})
print(B) # frozenset({frozenset({frozenset({'a'})})})

print(A is B)
# False

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({frozenset({'a'})})
print(B) # frozenset({frozenset({'a'})})

print(A is B)
# False

A = set(A).pop()
B = set(B).pop()

print(A) # frozenset({'a'})
print(B) # frozenset({'a'})

print(A is B)
# False
Enter fullscreen mode

Exit fullscreen mode



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *