[Patches] [ python-Patches-873305 ] list.__setitem__(slice) behavior

SourceForge.net noreply at sourceforge.net
Sun Mar 14 01:14:07 EST 2004


Patches item #873305, was opened at 2004-01-08 15:46
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=873305&group_id=5470

Category: None
Group: None
Status: Open
>Resolution: None
Priority: 5
Submitted By: Jonathan Brandmeyer (jbrandmeyer)
Assigned to: Nobody/Anonymous (nobody)
Summary: list.__setitem__(slice) behavior

Initial Comment:
Consider the following:
>>> x = [0, 1, 2, 3, 4]
>>> x[-1:0] = [5, 6, 7]

Currently the result is that x == [0,1,2,3,5,6,7,4]. 
However, I believe that calling setitem with an empty
slice should be a no-op, rather than performing an
insertion starting at the 'begin' index of the slice.

The following patch to Objects/listobject.c makes this
change.

Thanks,
Jonathan Brandmeyer

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-03-14 01:14

Message:
Logged In: YES 
user_id=80475

IMO, the risks of changing this outweight any possible
benefits.  I would be interested to see whether others would
rally for or against 
this one.

----------------------------------------------------------------------

Comment By: Jonathan Brandmeyer (jbrandmeyer)
Date: 2004-03-13 16:54

Message:
Logged In: YES 
user_id=676765

Here is a snippet from a conversation that occured on the
python-c++ list in January:

> So now the question is,
> what is the result of replacing an empty slice in a
container with a
> non-empty sequence? If the container supports insertion,
inserting the
> sequence seems logical enough to me.

So how do you define the point to perform the insertion?  If
the user asks to replace every element that is greater than
or equal to the fourth and less than the first element,
where do you place the new sequence?

The current action for a built-in list is to range-limit the
stop point to be not less than the start point, but I think
that is just for safety rather than an API decision, and
that it is wrong.

----------------------------------------------------------------------

Comment By: Jonathan Brandmeyer (jbrandmeyer)
Date: 2004-03-13 16:42

Message:
Logged In: YES 
user_id=676765

Consider this alternate example which should mean exactly
the same thing, but doesn't:
>>> x = [0, 1, 2, 3, 4]
>>> x[-1:0:1] = [5, 6, 7]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: attempt to assign sequence of size 3 to extended
slice of size 0


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-03-13 16:16

Message:
Logged In: YES 
user_id=80475

I believe we are stuck with this one.  Assigning to an empty
slice like a[2:2]=list('hotdog') is a valid and useful.  The
weird part is that the rightmost index gets adjusted to make
the most sense given the leftmost index.  In your example,
the offerred indices of -1 and 0 would seem to indicate a
programming error.  However, this autoadjusting is a long
standing feature and occassionally useful in its own right.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=873305&group_id=5470



More information about the Patches mailing list