What is the difference between using += and =+ in Python?

Asked

Viewed 10,568 times

8

I’m having a hard time understanding the difference in logic between:

x += n

And:

x =+ n
  • 2

    You saw the second option somewhere or tested it on your own and didn’t understand why the result is different?

  • By the way, I edited putting a title more in line with your question.

  • If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

3 answers

15

The answer is: depends on.

The answers of Maniero and of Luan are valid only when the variables in question are numerical.

The first code snippet, x += n, is the equivalent of x = x + n. The result will depend on who they are x and n. If they are numerical, they shall be summed; if they are strings will be concatenated; if lists are merged, etc. This is what we call in-place Operator.

In terms of Python API, do x += n is the same as x = x.__iadd__(n), that is, if x is instance of a user-defined class, the behavior of += may be different from "expected".

But the second part, x =+ n is the same as x = +n; that is, is doing an assignment in x with the return of the operator + in n. If n is numeric, returns the value itself without modifying the sign; if n for string make a mistake.

In terms of Python API, do +n is the same as pos(n), which is the same as n.__pos__(); that is, the value returned will depend on the method __pos__ of the object. Also, if n is defined by the user, the return of +n may be different from "expected".

All this behavior is defined by Data Model, in which the transfer of responsibility from language to the object is made. It is not the interpreter that will define what is the result of the operator, but the object itself; that is, it is possible to implement structures that completely escape the expected, as for example -x returning twice as many x, +x returning a list, etc.

class NonSense:
    def __init__(self, value):
        self.value = value

    def __neg__(self):
        return 2*self.value

    def __pos__(self):
        return [1, 2, 3]

x = NonSense(2)

print(-x)  # 4
print(+x)  # [1, 2, 3]
  • 1

    It is the most correct answer... +1

  • 1

    @fernandosavio Kiss ass xD

  • 1

    It has to be recorded before they mark the most voted as correct. hahaha

6

I am considering that using a standard numeric type existing in Python, with another type of data can give another result or error.

The first is adding up the value of n at value x existing and storing in xsame, because this is the compound operator of addition, therefore it is an accumulator, it is not a simple addition because it stores the value, it makes an assignment together of the addition. It’s the same as doing:

x = x + n

The second should be written in another way to be more readable:

x = +n

Or better yet:

x = n

which is the same thing, the signal there does not do something useful and does not need or should not be used.

When you write =+ is not using an operator, but rather two, without a space to separate them. Which is the opposite of += which is a single operator. There is a definition in the grammar of the language that indicates this. Which is why I think some languages should require space in certain places, so it wouldn’t create confusion.

The + there is a unary operator (has only one operand).

This code is only assigning (=) the value of n in the variable x. The sign of + there is no addition is just to confirm that the value must remain equal to what it was, just as you can use - to indicate that the value is negative, so it is a signal inverter.

It has no function at all. Some may find it more readable, but this question shows that it confuses more than it helps. Some languages are preferring not to be able to write a code like this (some regretted having created this useless operator), so not being able to put a + before values or variables only to indicate the sign and not being part of an arithmetic addition operation.

This so-called operator is only a signal verifier, that is, the value will keep the signal it already had, so if it was positive it is still positive and if it was negative it is still negative. And there’s another operator before that is simple assignment.

The operator that reverses the signal has utility, it would be something like this:

x = -n

I put in the Github for future reference.

In this case would be assigning to x the inverted value of n, therefore it would be a negative value if n positive or positive if n were negative.

It’s pure mathematics, more with more gives more, more with less gives less (no matter which comes first), and less with less gives more.

  • 3

    Although never seen in real code, it is worth mentioning that +n can be overloaded if the object implements the method __pos__ - then, strictly speaking +n and n are not equivalent. (this comment is enough to mark this difference however - the text of the reply is pretty cool already).

  • @jsbueno so I put that only goes for the numerical types of the language. There is another answer that already completes for these possible theoretical cases but that do not actually happen and that already has other answers (the focus here is the unary operator). Maybe someone saw as a mistake I did not describe in detail the exception and so negatived.

3

1.x += n is an abbreviation of x = x + n

2.x =+ n is an abbreviation of x = (+n)

then the first indicates the sum of the attribute x with the attribute n, the second indicates that the attribute x will receive the value n positively, for example

x =+ 5 element x will receive 5 as +5 because the =+to indicate the value as positive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.