pythonoperatorsincrement

Why does Python seem to treat =+ as a valid operator?


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. :)


Solution

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