Tuple slicing not returning a new object as opposed to list slicing

后端 未结 4 1022
抹茶落季
抹茶落季 2021-02-19 14:18

In Python (2 and 3). Whenever we use list slicing it returns a new object, e.g.:

l1 = [1,2,3,4]
print(id(l1))
l2 = l1[:]
print(id(l2))

Output

4条回答
  •  滥情空心
    2021-02-19 15:02

    Not sure about this but it seems that Python provides you with a new pointer to the same object to avoid copying since the tuples are identical (and since the object is a tuple, it's immutable).

提交回复
热议问题