0

What does the python operator =- do? I'm not asking about the -= operator, which I realize is shorthand for x = x - value.

3
  • 1
    Have you tested it? Break it down, don't think about it as 1 operator but two: = and -. Thus x =- x is negating x as a number. Commented May 26, 2017 at 13:25
  • @Roberto What do you mean "not valid python syntax"? It's completely valid. Commented May 26, 2017 at 13:26
  • I could swear there is a dupe for this... Commented May 26, 2017 at 13:26

3 Answers 3

7

Actually, the operator =- does not exist. It is only = (- value). So the negative of the value.

Example:

>>> x =- 1
>>> x
-1
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, x =- 1 just looks weird.
2

Why not test it?

In [11]: x = 1

In [12]: y = 2

In [13]: y=-x

In [14]: y
Out[14]: -1

As you can see it does nothing, but sets a negative value of the variable on the right hand side

Comments

1

There is no =- operator. Depending on the context this might be two operators, e.g. x =- y is equivalent to x = (-y) (so there are two operators: assignment and negation) or an assignment with a negative constant: x =- 1 is equivalent to x = (-1) (in this context - is not an operator, it's just a negative constant).

1 Comment

@timgeb github.com/python/cpython/blob/… As you can see - is parsed directly, there is no __neq__ call in case of constants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.