Обложка канала

Библиотека Python разработчика

20835 @BookPython

Библиотека Python разработчика. Книги по программированию на Python.

Библиотека Python разработчика

4 года назад
Открыть в
The default list slicing in Python creates copies. It may be undesirable if a sliced part is too big to be copied, you want it to reflect changes in the list, or even want to modify the slice to affect the original object. To solve the problem with copying a lot of data, one can use itertools.islice. It lets you iterate over the part of the list, but doesn't support indexing or modification. To achieve more than this, we have to write a custom class. Luckily Python provides the suitable abstract base class: collections.abc.MutableSequence. You only need to override __getitem__, __setitem__, __delitem__, __len__ and insert. This is the example of how you do it. It doesn't support deletion and inserting, but supports slicing slices and modifications.
slice_view

A Python repl by VadimPushtaev

replit