[Tutor] 'slice', etc
spir
denis.spir at gmail.com
Fri Dec 6 16:39:28 CET 2013
Hello,
How does slicing in Python really work? Apparently, there are slice objects
(start, past-end, step), generated using either the 'slice' builtin func or the
extended slicing syntax [i:k:s]. Is this correct? [1]
Does (only) the extended syntax (always) trigger slicing instead of contructing
a new subsequence? (new substring or sublist or whatever, actually holding a
section of the original sequence)
Are slices and subsequences transparently usable one for the other?
Does python internally choose whether it is best to do make a slice or
subsequence? If yes, does it also at times conversely make a slice instead of a
subsequence, when using the shorter syntax [i:k] (or even [i] for s string)? [2]
PS: I searched but could not find a PEP specifically dedicated to 'slice' only.
Pointers welcome, especially to docs explaining purpose, rationale, design,
etc... Maybe this means slices have always existed and I just missed them totally?
Thank you,
Denis
[1] I discovered that by chance; apparently, this is recent or I missed the
feature when I used to code in python commonly, years, ago. I only noted it now
because my favorite editor highlighted 'slice' as a Py builtin id!
[2] The fact that slices exist at all shows how worth it is to avoid needlessly
creating subsequences, at least in whole categories of case; but this is
certainly not always true, especially for very short strings, maybe other short
subseqs of short items.
[3] PEP 357 "Allowing Any Object to be Used for Slicing" is an extension about
__index__.
http://www.python.org/dev/peps/pep-0357/
More information about the Tutor
mailing list