Odd slicing behavior?

Diez B. Roggisch deets at web.de
Wed Jun 22 16:29:18 EDT 2005


Dave Opstad wrote:
> In article <3htv6gFips59U1 at uni-berlin.de>,
>  "Diez B. Roggisch" <deets at web.de> wrote:
> 
> 
>>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"
> 
> 
> But it isn't forbidden:
> 
> 
>>>>v = range(10)
>>>>v
> 
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> 
>>>>v[0:10:3] = ['a', 'b', 'c', 'd']
>>>>v
> 
> ['a', 1, 2, 'b', 4, 5, 'c', 7, 8, 'd']
> 
> The only time it's actively forbidden is when the length of the slice 
> and the length of the list being assigned to it don't match.

Oh, cool. But then the semantics are clear: if using [a:b:c] syntax, the 
  replaced slice has to be of same lenght. Makes sense - and in that 
case, I'd prefer c==1 not to be special cased - for the sake of a more 
clearly defined behaviour.

Regards,

Diez



More information about the Python-list mailing list