classification
Title: Misleading information about slice assignment in docs
Type: enhancement Stage:
Components: Documentation Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ezio.melotti, stefanchrobot, terry.reedy
Priority: normal Keywords:

Created on 2013-05-08 21:31 by stefanchrobot, last changed 2013-05-10 19:45 by terry.reedy.

Messages (2)
msg188738 - (view) Author: Stefan Chrobot (stefanchrobot) Date: 2013-05-08 21:31
http://docs.python.org/3/reference/simple_stmts.html#assignment-statements

The docs says:

"If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type."

This seems wrong, because the assigned object can be any iterable:

a = [4, 5, 6]
a[0:0] = range(1, 4)
# a is now [1, 2, 3, 4, 5, 6]
msg188870 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-05-10 19:45
Since range objects have a known length, that example is not enough to show 'any iterable'. However, generators do not even have a __length_hint__ and they work too.

a = [1,2,3]
a[0:1] = (i for i in range(4))
print(a)
>>> 
[0, 1, 2, 3, 2, 3]
History
Date User Action Args
2013-05-10 19:45:39terry.reedysetnosy: + terry.reedy
messages: + msg188870
2013-05-08 21:48:01ezio.melottisetnosy: + ezio.melotti
2013-05-08 21:31:39stefanchrobotcreate