Behaviour of increment and decrement operators in Python

后端 未结 9 1860
无人共我
无人共我 2020-11-22 05:26

I notice that a pre-increment/decrement operator can be applied on a variable (like ++count). It compiles, but it does not actually change the value of the vari

相关标签:
9条回答
  • 2020-11-22 05:59

    While the others answers are correct in so far as they show what a mere + usually does (namely, leave the number as it is, if it is one), they are incomplete in so far as they don't explain what happens.

    To be exact, +x evaluates to x.__pos__() and ++x to x.__pos__().__pos__().

    I could imagine a VERY weird class structure (Children, don't do this at home!) like this:

    class ValueKeeper(object):
        def __init__(self, value): self.value = value
        def __str__(self): return str(self.value)
    
    class A(ValueKeeper):
        def __pos__(self):
            print 'called A.__pos__'
            return B(self.value - 3)
    
    class B(ValueKeeper):
        def __pos__(self):
            print 'called B.__pos__'
            return A(self.value + 19)
    
    x = A(430)
    print x, type(x)
    print +x, type(+x)
    print ++x, type(++x)
    print +++x, type(+++x)
    
    0 讨论(0)
  • 2020-11-22 06:00

    ++ is not an operator. It is two + operators. The + operator is the identity operator, which does nothing. (Clarification: the + and - unary operators only work on numbers, but I presume that you wouldn't expect a hypothetical ++ operator to work on strings.)

    ++count
    

    Parses as

    +(+count)
    

    Which translates to

    count
    

    You have to use the slightly longer += operator to do what you want to do:

    count += 1
    

    I suspect the ++ and -- 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:

    • Simpler parsing. Technically, parsing ++count is ambiguous, as it could be +, +, count (two unary + operators) just as easily as it could be ++, count (one unary ++ operator). It's not a significant syntactic ambiguity, but it does exist.
    • Simpler language. ++ is nothing more than a synonym for += 1. It was a shorthand invented because C compilers were stupid and didn't know how to optimize a += 1 into the inc 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.
    • Confusing side-effects. One common newbie error in languages with ++ 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 precedence issues of pre-/post-increment in C are pretty hairy, and incredibly easy to mess up.
    0 讨论(0)
  • 2020-11-22 06:04

    In Python, a distinction between expressions and statements is rigidly enforced, in contrast to languages such as Common Lisp, Scheme, or Ruby.

    Wikipedia

    So by introducing such operators, you would break the expression/statement split.

    For the same reason you can't write

    if x = 0:
      y = 1
    

    as you can in some other languages where such distinction is not preserved.

    0 讨论(0)
提交回复
热议问题