sequence multiplied by -1

Terry Reedy tjreedy at udel.edu
Sat Sep 25 19:24:17 CEST 2010


On 9/25/2010 4:22 AM, Yingjie Lan wrote:
> Hi,
>
> I noticed that in python3k, multiplying a sequence by a negative
> integer is the same as multiplying it by 0, and the result is an
> empty sequence.

This is explicitly documented: "Values of n less than 0 are treated as 0 
(which yields an empty sequence of the same type as s).) I would have 
made this raise a ValueError, but someone must have had a use case for 
getting an empty sequence.

> It seems to me that there is a more meaningful symantics.
> Simply put, a sequence multiplied by -1 can give a reversed
> sequence.

There is already a builtin reversed() function whose output can be 
multiplied.

-- 
Terry Jan Reedy




More information about the Python-list mailing list