On Wed, Nov 29, 2017 at 5:46 AM, Alon Snir <[email protected]> wrote: > I would like to mention that the issue of assignment to a target list, is > also relevant to the case of elementwise assignment to a mutable sequence > (e.g. lists and arrays). Here as well, the rhs of the assignment statement > states the number of elements to be assigned. Consequently, the following > example will get into infinite loop: > >>>> from itertools import count >>>> A = [] >>>> A[:] = count() > > Writing "A[:2] = count()" will cause the same result. > Here as well, it is currently safer to use islice if the rhs is a generator > or an iterator. e.g.: > >>>> it = count() >>>> A[:] = islice(it,2) > > In my opinion, it is be better to devise a solution that could be applied in > both cases. Maybe a new kind of assignment operator that will be dedicated to > this kind of assignment. i.e. elementwise assignment with restriction on the > number of elements to be assigned, based on the length of the lhs object (or > the number of targets in the target list). >
Hmm. The trouble is that slice assignment doesn't have a fixed number of targets. If you say "x, y = spam", there's a clear indication that 'spam' needs to provide exactly two values; but "A[:] = spam" could have any number of values, and it'll expand or shrink the list accordingly. ChrisA _______________________________________________ Python-ideas mailing list [email protected] https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
