I wanted to extend the class list in python37 with some custom methods.
and ended up reading the UserList cpython code. After reading it new questions arose with regards of [:]
usage.
If I understand correctly the `[:]` makes a slice copy of the whole `self.data`. But I am trying to see what is the point of using `[:]` at the left side of the `=` operator.
Is there any difference between option one and two? Tried in the python interpreter, and both seem to have the same effect, am I missing something?
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# option (1)
letters[:] = []
# option (2)
letters = []
Now it comes my questions with regards to UserList code. I added comments with questions I have.
class UserList(_collections_abc.MutableSequence):
def __init__(self, initlist=None):
self.data = []
if initlist is not None:
if type(initlist) == type(self.data):
# NOTE: Is this if statement doing the same?
# if isinstance(initlist, list):
self.data[:] = initlist
# NOTE: wouldn't in this case self.data keep a reference to initlist
# instead of a copy?
# self.data[:] = initlist[:] # could one replace that line with this one?
elif isinstance(initlist, UserList):
self.data[:] = initlist.data[:]
# NOTE: would this line accomplish the same?
# self.data = initlist.data[:]
else:
self.data = list(initlist)
...