Assignment to slice

sambo sambo at void.com
Fri Jan 23 12:33:40 EST 2004



Rich Krauter wrote:

> I'm just surprised that nobody thinks it's unusual.
>
> >>> x = []
> >>> x[4] = 5 
> Python says "Wrong! Can't do that! I'm quitting now!"
> >>> x = []
> >>> x[4:5] = [1,2,3,4,5]
> Python says "Ok. Cool. Those indices don't exist in x so I'll just 
> stick your list at the beginning,
> and the indices you used in the slice have nothing to do with where 
> the elements end up,
> since they don't exist in the list anyway."
>
> To me that doesn't look much different than if python were to do this: 
> (not real code obviously)
> <fiction>
> >>> x = []
> >>> x[4] = 5
> >>> x
> [5]
> Python says "Well, x[4] doesn't exist so you must have meant x[0]."
> <fiction>
>
> That would make no sense. So why does it make sense to do that same 
> exact thing if you're
> assigning to a slice, instead of an element?
>
> That's what I see as inconsistent; not that python does not behave 
> just like perl. I'm sure that
> a little more time will reveal this 'inconsistency' as a shortcoming 
> in the wiring of my brain, and
> not a weird python idiosyncrasy. It's cool to hear other people's take 
> on it.
> Thanks alot.
> Rich
>
>
Well one might argue, I suppose, that the key word here is LIST - good 
for storing strings not so for numeric data/matrixes.

Imagine my surprise when I was trying to find my numeric command line 
arguments with int()
only to find int( "string" ) != 0 but an exception is raised instead.

Cheers




More information about the Python-list mailing list