11

I accidentally wrote:

total_acc =+ accuracy

instead of:

total_acc += accuracy

I searched the net and could not find anything. So what happened, why does Python think I mean what I am typing?

Computers trust us too much. :)

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    `total_acc =+ accuracy` is `total_acc = +accuracy` – Clodion Feb 05 '16 at 19:19
  • Oh so when you want the negative value, you don't do *(-1), thanks @Clodion! Didn't know that. – gsamaras Feb 05 '16 at 19:20
  • 4
    Possible duplicate of [Logic regarding the use of spaces with Unary Operators in Python3](http://stackoverflow.com/questions/35208351/logic-regarding-the-use-of-spaces-with-unary-operators-in-python3) – zondo Feb 05 '16 at 19:22
  • 1
    There ain't any changes regarding this from 2 to 3. But (imo) this post has way better answers. Hence I am not marking this as dupe. (If there is a better dupe, then I have no probs) – Bhargav Rao Feb 05 '16 at 19:27
  • Also @gsamaras `total_acc = ​ ​ ​ ​ + ​ ​ ​ ​ ​ accuracy` would work ;) – Bhargav Rao Feb 05 '16 at 19:35
  • Oh why the downvote? If it's something that should be improved, please let me know! Why @BhargavRao? – gsamaras Feb 05 '16 at 19:39
  • @BhargavRao I do not care about the rep, I just want to know if the question has something bad that the future reader should not read, that's why I am asking and I did not imply that you did. :) Can you explain why this will work? – gsamaras Feb 05 '16 at 19:42
  • 2
    Since you can put as many unary operators in a row as you want, you can really annoy your code reviewers with `total_sec+=-+-+-+-+-+-+-+-+-+-+-+-accuracy`. – tdelaney Feb 05 '16 at 19:42
  • 1
    That's almost as evil as it is the greek semicolon in my C++ code @tdelaney. :) – gsamaras Feb 05 '16 at 19:43
  • @gsamaras Python ignores all the spaces between operators and operands. Thus even your print statements can look like `print ( ​ ​ ​ ​ "something" ​ ​ ​ ​ ​)`. – Bhargav Rao Feb 05 '16 at 19:48

3 Answers3

9

If you are interested in catching this type of errors early, you can do that with static code analysis. For example, flake8:

$ cat test.py
total_acc = 0
accuracy = 10

total_acc =+ accuracy
$ flake8 test.py
test.py:4:12: E225 missing whitespace around operator

In this case, it is complaining about the extra space after the +, thinking that you actually meant total_acc = +accuracy. This would have helped you to discover the problem earlier.

FYI, pylint would catch that too.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
6

This is the same as if you were to do like total_acc = -accuracy, except positive instead of negative. It basically is the same as total_acc = accuracy though, as adding a + before a value does not change it.

This is called an unary operator as there is only one argument (ex: +a) instead of two (ex: a+b).

This link explains it a little more.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
John Howard
  • 61,037
  • 23
  • 50
  • 66
2

It thinks you're doing total_acc = +accuracy, which sets total_acc equal to accuracy. + before a variable without another value causes the variable's __pos__ method to be called. For most types, this is a nop, but there are certain types, e.g. Decimal that implement __pos__.

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
  • Yes that's correct, but what happens with `+` operator? – gsamaras Feb 05 '16 at 19:19
  • 3
    `+` before a value invokes the object's `__pos__()` method if it has one. Most types don't have such a method, so it's a no-op. `Decimal` is one type that does use it; https://stackoverflow.com/questions/16819023/whats-the-purpose-of-the-pos-unary-operator-in-python – kindall Feb 05 '16 at 19:23
  • @kindall Huh, I did not know that. Good to know, thanks! I'll add that to my answer. – Morgan Thrapp Feb 05 '16 at 19:24