This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Mutable Sequence Type works different for lists and bytearrays in slice[i:j:k]
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, py.user, serhiy.storchaka, siegfried.gevatter, terry.reedy, twouters
Priority: normal Keywords: patch

Created on 2011-07-21 22:31 by py.user, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
slice_del.diff siegfried.gevatter, 2015-04-13 14:08 Patch review
slice_del.diff siegfried.gevatter, 2015-04-13 14:13 patch including doc change review
slice_del.diff siegfried.gevatter, 2015-04-13 16:49 review
slice_del.diff siegfried.gevatter, 2015-04-13 16:54 simplified test review
slice_del.diff siegfried.gevatter, 2015-04-13 17:54 version without double type cast review
Messages (9)
msg140832 - (view) Author: py.user (py.user) * Date: 2011-07-21 22:31
>>> barr = bytearray(b'abcde')
>>> lst = list('abcde')
>>> barr[::-3] = ()
>>> barr
bytearray(b'acd')
>>> lst[::-3] = ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: attempt to assign sequence of size 0 to extended slice of size 2
>>> del lst[::-3]
>>> lst
['a', 'c', 'd']
>>>

lst[::-3] = () - is more convenient way for deletion
msg140835 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-07-21 23:18
I happen to prefer del myself, but I agree that the two mutable sequence classes should behave the same. The manual 4.6.4 says

s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t (1) 
1. t must have the same length as the slice it is replacing.

So the list behavior is not a bug. Extending its behavior is a feature request that could only happen in the 'next' release, now 3.3.

It is not usually considered a bug for something to do something beyond what is promised; in any case, you are *not* requesting that bytearrays be restricted as lists are. But that could be the fix to remove the 'bug' of inconsistency.  That also could not happen until a next release.

Since deletion of contiguous slices works and since deletion of extended slices can work and since I found the restriction slightly surprising, I am in favor of extending list behavior unless there is some internal reason why it cannot be.

The rationale for the restriction is that replacing a contiguous slice with a different number of items makes sense but replacing non-contiguous items with a different number does not -- except in the special case where the number is 0. So what you are really asking is that the footnote be changed to

1. t must have the same length as the slice it is replacing or be empty

That is the current behavior of bytearrays.
msg140845 - (view) Author: py.user (py.user) * Date: 2011-07-22 00:22
> I happen to prefer del myself
> but I agree that the two mutable sequence classes should behave the same.

del is not so flexible as assignement

>>> cmpr = [bytearray(i.encode('ascii')) for i in ('abcd', 'efgh', 'ijkl')]
>>> cmpr
[bytearray(b'abcd'), bytearray(b'efgh'), bytearray(b'ijkl')]
>>> cmpr[0][::-2] = cmpr[1][::2] = cmpr[2][1::2] = ()
>>> cmpr
[bytearray(b'ac'), bytearray(b'fh'), bytearray(b'ik')]
>>>
msg140846 - (view) Author: py.user (py.user) * Date: 2011-07-22 00:29
the former could be like:
del cmpr[0][::-2], cmpr[1][::2], cmpr[2][1::2]

it's ok

how to implement this with del ?
>>> cmpr
[bytearray(b'abcd'), bytearray(b'efgh'), bytearray(b'ijkl')]
>>> cmpr[0][::-2], cmpr[1][::2] = (), cmpr[2][1::2]
>>> cmpr
[bytearray(b'ac'), bytearray(b'jflh'), bytearray(b'ijkl')]
>>>
msg240586 - (view) Author: Siegfried Gevatter (siegfried.gevatter) Date: 2015-04-13 02:32
I'll have a try at this one.
msg240635 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2015-04-13 15:58
I left some style comments in the rietveld review. However, a bigger questions is whether we should change list at all. I think we should; the change is fairly straightforward, there is some value in it and it's a good idea to keep bytearray and other mutable sequences behave similarly (and we can't remove the support from bytearray for obvious backward compatibility reasons.)
msg241605 - (view) Author: Siegfried Gevatter (siegfried.gevatter) Date: 2015-04-20 05:27
Thomas, could you take another look? I did update the patch.

Thanks! :)
msg257341 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-02 10:04
Note that adding this new feature to list adds a pressure to add this feature to other mutable sequences (deque, ElementTree.Element, etc). This is a burden for maintainers.

array already supports this feature as well as bytearray. It may be a difference between arrays of unboxed values and general collections of Python objects.
msg257407 - (view) Author: py.user (py.user) * Date: 2016-01-03 09:27
Also memoryview() doesn't support:

>>> m = memoryview(bytearray(b'abcde'))
>>> m[::2] = ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' does not support the buffer interface
>>> m[::2] = b''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: memoryview assignment: lvalue and rvalue have different structures
>>> m[::2] = b'123'
>>> m.tobytes()
b'1b2d3'
>>>
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56815
2016-01-03 09:27:27py.usersetmessages: + msg257407
2016-01-02 10:43:23petri.lehtinensetnosy: - petri.lehtinen
2016-01-02 10:04:23serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg257341
2016-01-02 09:05:45ezio.melottisetversions: + Python 3.6
2015-04-20 20:45:28terry.reedysetstage: test needed -> patch review
versions: + Python 3.5, - Python 3.4
2015-04-20 05:27:21siegfried.gevattersetmessages: + msg241605
2015-04-13 17:54:08siegfried.gevattersetfiles: + slice_del.diff
2015-04-13 16:54:32siegfried.gevattersetfiles: + slice_del.diff
2015-04-13 16:49:55siegfried.gevattersetfiles: + slice_del.diff
2015-04-13 15:58:48twouterssetnosy: + twouters
messages: + msg240635
2015-04-13 14:13:20siegfried.gevattersetfiles: + slice_del.diff
2015-04-13 14:08:36siegfried.gevattersetfiles: + slice_del.diff
keywords: + patch
2015-04-13 02:32:16siegfried.gevattersetnosy: + siegfried.gevatter
messages: + msg240586
2013-07-07 13:37:06ezio.melottisetnosy: + ezio.melotti

versions: + Python 3.4, - Python 3.3
2011-07-22 18:07:51petri.lehtinensetnosy: + petri.lehtinen
2011-07-22 00:29:53py.usersetmessages: + msg140846
2011-07-22 00:22:44py.usersetmessages: + msg140845
2011-07-21 23:18:44terry.reedysetversions: + Python 3.3, - Python 3.1
nosy: + terry.reedy

messages: + msg140835

type: behavior -> enhancement
stage: test needed
2011-07-21 22:31:34py.usercreate