code_junkie

Python list slice syntax used for no obvious reason

Nov 12th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. newList = oldList[:]
  2.  
  3. newList = oldList
  4.  
  5. # ================================================================
  6. # === ShallowCopy.py =============================================
  7. # ================================================================
  8. #
  9. class Foo:
  10. def __init__(self, data):
  11. self._data = data
  12.  
  13. aa = Foo ('aaa')
  14. bb = Foo ('bbb')
  15.  
  16. # The initial list has two elements containing 'aaa' and 'bbb'
  17. OldList = [aa,bb]
  18. print OldList[0]._data
  19.  
  20. # The shallow copy makes a new list pointing to the old elements
  21. NewList = OldList[:]
  22. print NewList[0]._data
  23.  
  24. # Updating one of the elements through the new list sees the
  25. # change reflected when you access that element through the
  26. # old list.
  27. NewList[0]._data = 'xxx'
  28. print OldList[0]._data
  29.  
  30. # Updating the new list to point to something new is not reflected
  31. # in the old list.
  32. NewList[0] = Foo ('ccc')
  33. print NewList[0]._data
  34. print OldList[0]._data
  35.  
  36. >>> # ================================================================
  37. ... # === ShallowCopy.py =============================================
  38. ... # ================================================================
  39. ... #
  40. ... class Foo:
  41. ... def __init__(self, data):
  42. ... self._data = data
  43. ...
  44. >>> aa = Foo ('aaa')
  45. >>> bb = Foo ('bbb')
  46. >>>
  47. >>> # The initial list has two elements containing 'aaa' and 'bbb'
  48. ... OldList = [aa,bb]
  49. >>> print OldList[0]._data
  50. aaa
  51. >>>
  52. >>> # The shallow copy makes a new list pointing to the old elements
  53. ... NewList = OldList[:]
  54. >>> print NewList[0]._data
  55. aaa
  56. >>>
  57. >>> # Updating one of the elements through the new list sees the
  58. ... # change reflected when you access that element through the
  59. ... # old list.
  60. ... NewList[0]._data = 'xxx'
  61. >>> print OldList[0]._data
  62. xxx
  63. >>>
  64. >>> # Updating the new list to point to something new is not reflected
  65. ... # in the old list.
  66. ... NewList[0] = Foo ('ccc')
  67. >>> print NewList[0]._data
  68. ccc
  69. >>> print OldList[0]._data
  70. xxx
  71.  
  72. >>> a = [1, 2, 3, 4]
  73. >>> b = a
  74. >>> c = a[:]
  75. >>> b[2] = 10
  76. >>> c[3] = 20
  77. >>> a
  78. [1, 2, 10, 4]
  79. >>> b
  80. [1, 2, 10, 4]
  81. >>> c
  82. [1, 2, 3, 20]
  83.  
  84. def myfunction(mylist=[]):
  85. pass
  86.  
  87. a = ['one','two','three']
  88.  
  89. b = a[:]
  90.  
  91. b[1] = 2
  92.  
  93. print id(a), a #Output: 1077248300 ['one', 'two', 'three']
  94. print id(b), b #Output: 1077248908 ['one', 2, 'three']
  95.  
  96. a = ['one','two','three']
  97.  
  98. b = a
  99.  
  100. b[1] = 2
  101.  
  102.  
  103. print id(a), a #Output: 1077248300 ['one', 2, 'three']
  104. print id(b), b #Output: 1077248300 ['one', 2, 'three']
Add Comment
Please, Sign In to add comment