sequence multiplied by -1

Y

Yingjie Lan

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
 
A

Arnaud Delobelle

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.
 
B

BartC

Yingjie Lan said:
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?

Gimmicky.

Best to define multiplication only by unsigned or positive values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top