Odd slicing behavior?

Diez B. Roggisch deets at web.de
Wed Jun 22 16:15:12 EDT 2005


Dave Opstad wrote:
> So given the equality of their slice representations, why do the v2[::] 
> and v2[0:10:1] assignments behave differently? My reading of section 
> 5.3.3 of the manual suggests that these should behave the same.
> 
> If I'm just not seeing something obvious, please let me know!

It's not so much obvious - I had to experiment myself a little bit. This 
works:

v[0:10] = [1]

So what can we conclude from that? It seems that what is implied by the 
slice-assignment syntax is that the slice is contingous. The reason 
becomes obvious when one does something like this:

v[0:10:2] = [1,2]

What do you expect to happen in this case?

So - the rationale seems to be: "When using slice-assignment, a form 
like l[a:b:c] imposes possibly a non-continous section in l, for which 
the semantics are unclear - so we forbid it"

It should be mentioned in the docs, though - and on could argue if 
[a:b:c] == [a:b] could be checked for at runtime (c being 1). But then - 
if you really wanted that, you could have written [a:b] in the first place.

So - I'd say it's an doc-error at most :)

Diez



More information about the Python-list mailing list