[Tutor] 'slice', etc

eryksun eryksun at gmail.com
Sat Dec 7 13:12:54 CET 2013


On Sat, Dec 7, 2013 at 6:04 AM, spir <denis.spir at gmail.com> wrote:
> I knew about the common sense of slice in Python as a sysnonym of
> subsequence, meaning a partial copy; so I thought this different sense I
> just discovered, about slice _objects_ properly in Python, was about slice
> as commonly understood in other languages (D, C++, many more); but this is
> yet another, third meaning! ;-). Like a slice sense2, but without pointer to
> the original object.]
>
> They are more like (x)ranges, thus: an abstract representation of an
> interval.

There are also itertools.islice objects that actually reference an iterable:

    >>> from itertools import islice
    >>> s = 'The quick brown fox...'
    >>> s1 = islice(s, 0, 10)
    >>> s2 = islice(s, 10, None)
    >>> ''.join(s1)
    'The quick '
    >>> ''.join(s2)
    'brown fox...'

It works by getting an iterator and skipping items up to the start
index. You can't reuse an islice iterator:

    >>> ''.join(s1)
    ''


More information about the Tutor mailing list