Przeczytałem coś o plasterku w python3. Potem napisałem program, próbowałem wdrożyć getitem (self, plaster (y)). Kod spadnie poniżej:[python]: jak zaimplementować plaster w python3?
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
potem znalazłem się x.step jest None, co czyniło range podnieść wyjątek. Jak zatem wdrożyć metodę getitem?
Twoja klasa 'NewList' powinna dziedziczyć z' list' ... – JBernardo
@JBernardo - Czy chcesz uniknąć jawnego używania obiektów slice? – Alcott