Assignment to slice

Rich Krauter rmkrauter at yahoo.com
Thu Jan 22 00:23:12 CET 2004


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040121/674fb6c0/attachment.html>


More information about the Python-list mailing list