Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>++</code> is not an operator. It is two <code>+</code> operators. The <code>+</code> operator is the <strong>identity</strong> operator, which does nothing. (Clarification: the <code>+</code> and <code>-</code> unary operators only work on numbers, but I presume that you wouldn't expect a hypothetical <code>++</code> operator to work on strings.)</p> <pre><code>++count </code></pre> <p>Parses as</p> <pre><code>+(+count) </code></pre> <p>Which translates to</p> <pre><code>count </code></pre> <p>You have to use the slightly longer <code>+=</code> operator to do what you want to do:</p> <pre><code>count += 1 </code></pre> <p>I suspect the <code>++</code> and <code>--</code> operators were left out for consistency and simplicity. I don't know the exact argument Guido van Rossum gave for the decision, but I can imagine a few arguments:</p> <ul> <li>Simpler parsing. Technically, parsing <code>++count</code> is ambiguous, as it could be <code>+</code>, <code>+</code>, <code>count</code> (two unary <code>+</code> operators) just as easily as it could be <code>++</code>, <code>count</code> (one unary <code>++</code> operator). It's not a significant syntactic ambiguity, but it does exist.</li> <li>Simpler language. <code>++</code> is nothing more than a synonym for <code>+= 1</code>. It was a shorthand invented because C compilers were stupid and didn't know how to optimize <code>a += 1</code> into the <code>inc</code> instruction most computers have. In this day of optimizing compilers and bytecode interpreted languages, adding operators to a language to allow programmers to optimize their code is usually frowned upon, especially in a language like Python that is designed to be consistent and readable.</li> <li>Confusing side-effects. One common newbie error in languages with <code>++</code> operators is mixing up the differences (both in precedence and in return value) between the pre- and post-increment/decrement operators, and Python likes to eliminate language "gotcha"-s. The <a href="http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm" rel="noreferrer">precedence issues</a> of <a href="https://stackoverflow.com/a/17819123/1449799">pre-/post-increment in C</a> are pretty hairy, and incredibly easy to mess up.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload