sequence multiplied by -1

Arnaud Delobelle arnodel at gmail.com
Sat Sep 25 16:38:56 EDT 2010


On 25 Sep, 09:22, Yingjie Lan <lany... at yahoo.com> 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. It seems to me that there is a more meaningful symantics.
>
> Simply put, a sequence multiplied by -1 can give a reversed sequence.
>
> Then for any sequence "seq", and integer n>0, we can have
>
> "seq * -n" producing "(seq * -1) * n".
>
> Any thoughts?
>
> Yingjie

If [1, 2]*-1 is correct, then, arguably, so should be -[1, 2] :)

Some answers have invoked mathematics to weigh the value of this
proposal, e.g. likening lists to vectors.  But the obvious
mathematical analogy is that the set of all lists forms a monoid under
the operation of concatenation, which (unfortunately?) is performed
with the "+" operator in Python. So it is natural that "*" represents
repeated concatenation.

Now under concatenation, non-empty lists do not have an inverse, i.e.
for any non-empty list l, there does not exist a list l' such that l +
l' == [].  So there is no natural interpretation of -l and therefore
of l*-1.

However, by using "+" for list (and string) concatenation, Python
already breaks the mathematical pledge of commutativity that this
operator implies.

--
Arnaud



More information about the Python-list mailing list