Doing some basic experiments about Python array slicing. Why the same level has the same id
(address), but the array value is different. Is the the mechanism of Python slicing, or it just how id()
works?
a = [1, 2, 3, 4, 5, 6]
def test2(a, level):
if not a: return
mid = len(a) // 2
print(level, a, id(a))
if len(a) > 1:
test2(a[:mid], level + 1)
test2(a[mid:], level + 1)
test2(a, 0)
## Result:
# 0 [1, 2, 3, 4, 5, 6] 4565360520
# 1 [1, 2, 3] 4565360456
# 2 [1] 4566307656
# 2 [2, 3] 4566307656
# 3 [2] 4566307592
# 3 [3] 4566307592
# 1 [4, 5, 6] 4565360456
# 2 [4] 4566307656
# 2 [5, 6] 4566307656
# 3 [5] 4566307592
# 3 [6] 4566307592
## Same level has same id??
Then when I append the [level, a, id(a)]
in to a stack then print it out.
The same level has different id now. No matter when I print it out in the function, or store in the stack, then print it out one by one.
a = [1, 2, 3, 4, 5, 6]
stack = []
def test2(a, level):
if not a: return
mid = len(a) // 2
print(level, a, id(a))
stack.append([level, a, id(a)])
if len(a) > 1:
test2(a[:mid], level + 1)
test2(a[mid:], level + 1)
test2(a, 0)
print("-----------------")
stack = sorted(stack)
for i in stack:
print(i)
'''
0 [1, 2, 3, 4, 5, 6] 4393918344
1 [1, 2, 3] 4394865352
2 [1] 4394510088
2 [2, 3] 4394865608
3 [2] 4394867592
3 [3] 4394867656
1 [4, 5, 6] 4394867912
2 [4] 4394868040
2 [5, 6] 4394868168
3 [5] 4394867720
3 [6] 4394868360
-----------------
[0, [1, 2, 3, 4, 5, 6], 4393918344]
[1, [1, 2, 3], 4394865352]
[1, [4, 5, 6], 4394867912]
[2, [1], 4394510088]
[2, [2, 3], 4394865608]
[2, [4], 4394868040]
[2, [5, 6], 4394868168]
[3, [2], 4394867592]
[3, [3], 4394867656]
[3, [5], 4394867720]
[3, [6], 4394868360]
'''