Is there a compound assignment operator for a = b <operator> a (where <operator> is not commutative)?
Asked Answered
P

3

5

In a lot of languages a = a + b can be written as a += b In case of numerical operations, a + b is same as b + a, so the single compound operator suffices.

Also, a = a - b can be written as a -=b .

However, a-b is not equal to b-a. Hence, the compound assignment operator does not work for a = b - a

So, are there compound assignment operators for the operation a = b op a (where op can be +, -, *, /, %, and order matters) ?

[Non commutative operations]

Pili answered 12/11, 2012 at 17:16 Comment(9)
That depends of the type of the object involved.Denote
I am asking for cases where the + operation is not commutative whatever the type of object.Pili
Just build a string by appending rather than prepending.Allow
what is it you are trying to do with the 2 strings other than concatenation?Gaur
@Imno Strings are just an example.Pili
btw, to avoid string-related confusion here in answers you could use /= or -= where the order of operands matter.Aubervilliers
@Aubervilliers thanks for the suggestion. Although, i personally dont see why everybody just jumped at the strings and not the actual question !!Pili
I don't think that there's notation for that, but as @MaximYegorushkin suggested, you could use =-, =%, etc. to denote it.Laceration
@Laceration that was exactly what i was thinking but it seems a little illogical :/Pili
K
3

No, there is not.


Origin of the shorthand

I suspect this shorthand to come from assembly language where the ADD instruction does exactly that - takes two operands, makes an addition and stores it to the first one.

I'd say people were used to think this way and so this pattern appeared also in C language as a += b shorthand. Other languages took this from C.

I think there is no special reason to have or not to have a = a + b or a = b + a. I think none of them two is more often needed in programming. The reason is historical. The same why we use QWERTY keyboard layout and not the others.

Update: See this, it is a myth, because C was based on B language rather than coming from assembly languages. The origin is not clear.

Possible reasons

  • Every operator makes the language more complex. Python supports operator overloading, so there is even more work to have a new one.
  • It is rarely used in comparing with +=.
  • People are (were) used from assembly language more to += kind of operation rather than a = b + a, so they were okay with the fact no shorthand existed and did not requested it.
  • Readability concerns.
  • Lack of suitable syntax. How would you design it?

Possible solutions

The best possible solution is to just write a = b + a, because it is clear and readable from the first glance. For the same reason (readability) (Update: who knows?) Python does not provide a++ known from C and other languages. You have to type a += 1. The += shorthand is not very readable to a programming beginner neither, but one can still somehow at least guess what is about. It is compromise between tradition, laziness and readability.

If there is no tradition, readability should win, at least in Python. So one should clearly write a few characters more rather than looking for a shorthand. That is the case for a = b + a.

Note

If you are concatenating more strings, you should watch for .join() for the performance concern.

Kaikaia answered 12/11, 2012 at 17:33 Comment(3)
Interesting (origins). Any thing/proof to support your thoughts ? I used to think the notation developed simply as a language feature because of the ease of writing. I personally dont think there should be any readability issues with this notation other than in the case of absolute beginners.Pili
I have no proof, but I tried to Google something and I stumbled upon this reasoning: #3655330 People mention unnecessary complexity, readability, etc. This answer somehow works with my thoughts by diving into history of C and linking some sources https://mcmap.net/q/54244/-why-are-there-no-and-operators-in-python, but it seems it is just a myth.Kaikaia
Thanks,especially the link :)Pili
M
4

No there isn't a short-hand notation for a = b + a. If you need to do a lot of a = b + a for strings, you'd better build a list like:

lst = []

...

lst.append("a")
lst.append("bb")
lst.append("ccc")
lst.append("dddd")

...

lst.reverse()

return ''.join(lst)   # "ddddcccbba"
Machute answered 12/11, 2012 at 17:22 Comment(1)
I am not specifically concerned with strings. It was just an example to explain the question.Pili
K
3

No, there is not.


Origin of the shorthand

I suspect this shorthand to come from assembly language where the ADD instruction does exactly that - takes two operands, makes an addition and stores it to the first one.

I'd say people were used to think this way and so this pattern appeared also in C language as a += b shorthand. Other languages took this from C.

I think there is no special reason to have or not to have a = a + b or a = b + a. I think none of them two is more often needed in programming. The reason is historical. The same why we use QWERTY keyboard layout and not the others.

Update: See this, it is a myth, because C was based on B language rather than coming from assembly languages. The origin is not clear.

Possible reasons

  • Every operator makes the language more complex. Python supports operator overloading, so there is even more work to have a new one.
  • It is rarely used in comparing with +=.
  • People are (were) used from assembly language more to += kind of operation rather than a = b + a, so they were okay with the fact no shorthand existed and did not requested it.
  • Readability concerns.
  • Lack of suitable syntax. How would you design it?

Possible solutions

The best possible solution is to just write a = b + a, because it is clear and readable from the first glance. For the same reason (readability) (Update: who knows?) Python does not provide a++ known from C and other languages. You have to type a += 1. The += shorthand is not very readable to a programming beginner neither, but one can still somehow at least guess what is about. It is compromise between tradition, laziness and readability.

If there is no tradition, readability should win, at least in Python. So one should clearly write a few characters more rather than looking for a shorthand. That is the case for a = b + a.

Note

If you are concatenating more strings, you should watch for .join() for the performance concern.

Kaikaia answered 12/11, 2012 at 17:33 Comment(3)
Interesting (origins). Any thing/proof to support your thoughts ? I used to think the notation developed simply as a language feature because of the ease of writing. I personally dont think there should be any readability issues with this notation other than in the case of absolute beginners.Pili
I have no proof, but I tried to Google something and I stumbled upon this reasoning: #3655330 People mention unnecessary complexity, readability, etc. This answer somehow works with my thoughts by diving into history of C and linking some sources https://mcmap.net/q/54244/-why-are-there-no-and-operators-in-python, but it seems it is just a myth.Kaikaia
Thanks,especially the link :)Pili
R
0

I don't know of such a shortcut built into any language, but some languages would allow you to create one.

In Scala, for instance, you can essentially define your own operators.

Rasping answered 12/11, 2012 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.