Python why would you use [:] over =

前端 未结 4 1084
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 04:36

I am just learning python and I am going though the tutorials on https://developers.google.com/edu/python/strings

Under the String Slices section

4条回答
  •  旧时难觅i
    2021-02-05 05:24

    If you have a list the result is different:

    l = [1,2,3]
    l1 = l
    l2 = l[:]
    

    l2 is a copy of l (different object) while l1 is an alias of l which means that l1[0]=7 will modify also l, while l2[1]=7 will not modify l.

提交回复
热议问题