*Memos:
- My post explains a set and copy.
- 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 copy of the set with a tuple.
- 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 variable assignment.
<A set with an iterator>
A set can have a tuple and iterator but cannot have a set, list and dictionary.
<Normal Copy>:
*Memos:
-
A
andB
refer to the same shallow set and deep iterator. -
is
keyword can check ifv1
andv2
refer to the same set or iterator.
# Shallow set # # ↓ ↓ A = {iter(['a'])} # ↑↑↑↑↑↑↑↑↑ # Deep iterator # B = A print(A) # {<list_iterator object at 0x000001F95F9C6410>} print(B) # {<list_iterator object at 0x000001F95F9C6410>} print(A is B) # True v = B.pop() print(v) # <list_iterator object at 0x000001F95F9C6410> print(A) # set() print(B) # set()
<Shallow Copy>:
copy() can do shallow copy as shown below:
*Memos:
-
A
andB
refer to different shallow sets. -
A
andB
refer to the same deep iterator.
A = {iter(['a'])} B = A.copy() # Here print(A) # {<list_iterator object at 0x000001F9606954E0>} print(B) # {<list_iterator object at 0x000001F9606954E0>} print(A is B) # False v1 = A.pop() v2 = B.pop() print(v1) # <list_iterator object at 0x000001F9606954E0> print(v2) # <list_iterator object at 0x000001F9606954E0> print(v1 is v2) # True print(next(v1)) # a print(next(v2)) # StopIteration:
The below with copy() which can do shallow copy is equivalent to the above:
from copy import copy A = {iter(['a'])} B = copy(A) # Here print(A) # {<list_iterator object at 0x000001F95F9CD510>} print(B) # {<list_iterator object at 0x000001F95F9CD510>} print(A is B) # False v1 = A.pop() v2 = B.pop() print(v1) # <list_iterator object at 0x000001F95F9CD510> print(v2) # <list_iterator object at 0x000001F95F9CD510> print(v1 is v2) # True print(next(v1)) # a print(next(v2)) # StopIteration:
The below with set() which can do shallow copy is equivalent to the above:
A = {iter(['a'])} B = set(A) # Here print(A) # {<list_iterator object at 0x000001F9605E3730>} print(B) # {<list_iterator object at 0x000001F9605E3730>} print(A is B) # False v1 = A.pop() v2 = B.pop() print(v1) # <list_iterator object at 0x000001F9605E3730> print(v2) # <list_iterator object at 0x000001F9605E3730> print(v1 is v2) # True print(next(v1)) # a print(next(v2)) # StopIteration:
<Deep Copy>:
deepcopy() can do deep copy:
*Memos:
-
A
andB
refer to the different shallow sets and deep iterators. -
deepcopy()
should be used because it's safe, doing copy deeply.
from copy import deepcopy A = {iter(['a'])} B = deepcopy(A) # Here print(A) # {<list_iterator object at 0x000001F96065DE40>} print(B) # {<list_iterator object at 0x000001F960695CC0>} print(A is B) # False v1 = A.pop() v2 = B.pop() print(v1) # <list_iterator object at 0x000001F96065DE40> print(v2) # <list_iterator object at 0x000001F960695CC0> print(v1 is v2) # False print(next(v1)) # a print(next(v2)) # a
The below with copy() and copy() which can do shallow copy is equivalent to the above:
from copy import copy A = {iter(['a'])} B = A.copy() # Here print(A) # {<list_iterator object at 0x000001F9604F6410>} print(B) # {<list_iterator object at 0x000001F9604F6410>} print(A is B) # False v1 = copy(A.pop()) # Here v2 = copy(B.pop()) # Here print(v1) # <list_iterator object at 0x000001F9604F7910> print(v2) # <list_iterator object at 0x000001F9605E3190> print(v1 is v2) # False print(next(v1)) # a print(next(v2)) # a
The below with set() and copy() which can do shallow copy is equivalent to the above:
from copy import copy A = {iter(['a'])} B = set(A) # Here print(A) # {<list_iterator object at 0x000001F95F9BDF90>} print(B) # {<list_iterator object at 0x000001F95F9BDF90>} print(A is B) # False v1 = copy(A.pop()) # Here v2 = copy(B.pop()) # Here print(v1) # <list_iterator object at 0x000001F96065DFF0> print(v2) # <list_iterator object at 0x000001F96065D360> print(v1 is v2) # False print(next(v1)) # a print(next(v2)) # a
Additionally, the below is the 3D set with a 2D iterator:
from copy import deepcopy A = {iter([iter(['a'])])} B = deepcopy(A) # Here print(A) # {<list_iterator object at 0x0000029DDF277B20>} print(B) # {<list_iterator object at 0x0000029DE0049210>} print(A is B) # False v1 = A.pop() v2 = B.pop() print(v1) # <list_iterator object at 0x0000029DDF277B20> print(v2) # <list_iterator object at 0x0000029DE0049210> print(v1 is v2) # False v3 = next(v1) v4 = next(v2) print(next(v3)) # a print(next(v4)) # a print(v3 is v4) # False
Top comments (0)