List slicing on Python 2.7

Arkadiusz Bulski arek.bulski at gmail.com
Thu Mar 15 12:07:14 EDT 2018


Found the answer on stack overflow. Some types on some runtimes (builtins
and on Python 2) use another method __getslice__ instead of __getitem__.
https://docs.python.org/2/reference/datamodel.html#object.__getslice__


czw., 15 mar 2018 o 12:54 użytkownik Arkadiusz Bulski <arek.bulski at gmail.com>
napisał:

> I have a custom class (a lazy list-like container) that needs to support
> slicing. The __getitem__ checks if index is of slice type, and does a list
> comprehension over individual integer indexes. The code works fine on
> Python 3 but fails on 2.7, both CPython and PyPy. The print inside
> __getitem__ doesnt even get executed when its a slice. Does 2.7 have
> different object model, where slices are handled by a different method than
> __getitem__?
>
> The implementation and error log
>
> https://github.com/construct/construct/blob/8839aac2b68c9e8240e9d9c041a196b0a7aa7d9b/construct/core.py#L4785-L4796
>
> https://github.com/construct/construct/blob/8839aac2b68c9e8240e9d9c041a196b0a7aa7d9b/tests/test_core.py#L1148
> https://travis-ci.org/construct/construct/jobs/353782126#L887
>
> --
> ~ Arkadiusz Bulski
>
-- 
~ Arkadiusz Bulski



More information about the Python-list mailing list