Assignment to slice

Josiah Carlson jcarlson at
Thu Jan 22 04:00:04 CET 2004

All 'different' language features are 'unusual' until people get used to
them.  While you are currently saying, "I can't believe people don't
think this is strange", the rest of us got over it, and may even be
using the feature to get the job done.

Hell, I think that Perl variables are f-ing weird.  In the dozen
languages I've learned over the years, Perl is the only one where you
have to say hey, you this variable I'm accessing now, it is a scalar,
not a string.  What the hell is that?  On the other hand, Perl users
have gotten used to it, and don't think it is strange.

The real difference is that I'm not hanging out on Perl newsgroups or
mailing lists saying, "Dude, this variable thing is wack" (this actually
has more to do with the fact that I don't much like Perl, and don't use
it, than anything else).  Get used to slices working the way they do, it
is not likely to change anytime soon.

 - Josiah

> 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
> On Wed, 2004-01-21 at 16:09, Josiah Carlson wrote:
> > > Instead python will just tack stuff on to the front of
> > > the array. Which, I still believe, is totally
> > > inconsistent. But what do I know? I have about 5
> > > minutes of python experience.
> > 
> > > Oh well. Maybe my problem is that I'm using perl as my
> > > model of consistency. Plus, it's a minor issue anyway.
> > > I just need to be aware of it, and not try to write
> > > perl code in python. I need to learn to write python
> > > code in python.
> > 
> > There are a few choices that one can make when allowing slice reading
> > and writing in sequences.  Python made one: never cause an exception for
> > integer arguments, and certain indices map to the same location.  Perl
> > made another: extend the sequence if necessary to fill it out.
> > 
> > Both are consistent and predictable.  Python does it one way, Perl does
> > it another.  Heck, Python has variable definitions that are done one way,
> > Perl has variable definitions that are done another.  They were choices
> > made during the creation that fit a paradigm.
> > 
> > Expecting Python to behave like Perl is like expecting your mother to
> > pee standing up; without alteration, it's not gonna happen.
> > 
> >  - Josiah

More information about the Python-list mailing list