I read something about slice in Python 3. Then I wrote a program, tried to implement __getitem__(self, slice(s)). Code goes below:
class NewList:
def __init__(self, lst):
print('new list')
self._list = lst
def __getitem__(self, x):
if type(x) is slice:
return [ self._list[n] for n in range(x.start, x.stop, x.step) ] #error?
else:
return self._list[x]
...
nl1 = NewList([1,2,3,4,5])
nl1[1:3] #error occurs
Then I found out x.step is None, which made range raise an exception.
So, how should I implement the __getitem__ method?
NewListclass should inherit fromlist...