[Tutor] 'slice', etc

spir denis.spir at gmail.com
Sat Dec 7 11:51:48 CET 2013


On 12/07/2013 02:49 AM, Joel Goldstick wrote:
>>> >>Hum, we are not talking of the same topic, apparently. I mean this, from
>>> >>the library ref, builtin funcs:
>>> >>http://docs.python.org/3.3/library/functions.html#slice:
>>> >>
>>> >>   slice(start, stop[, step])
>>> >>
>> >
> I'm totally confused by this.  What is this a slice of? there is no object
> on which to apply the slice indices?

Same for me, and unfortunately the docs do not say more apparently, about their 
purpose and usage, reason why I asked (!).

I'm used to slices in languages like D, which are the same things as arrays, 
meaning {start-pointer, length} structs (plus capacity if dynamic size). But the 
start pointer is not a start *array-index*, it really points into the array 
data. In Python, we'd need another field to point to the object (the original 
string or list or whatnot), or to its data zone in memory.

I guess Python slices (in this very sense) are to be used in the following case: 
when we happen to scan a big sequence into lots and lots of little slices. 
Instead of keeping
* either lots and lots of copies of the subsequences
* or lots and lots of slices, all pointed to the same original seq
we keep slices mentioning only the range (or interval) in the original seq, 
which remains implicit. (In a lower-level lang with pointers, there is no such 
difference.)

Makes sense, no? What do you think?

Denis




More information about the Tutor mailing list