Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

python - Python中递增和递减运算符的行为(Behaviour of increment and decrement operators in Python)

I notice that a pre-increment/decrement operator can be applied on a variable (like ++count ).

(我注意到可以将预增/减运算符应用于变量(例如++count )。)

It compiles, but it does not actually change the value of the variable!

(它可以编译,但实际上并不会改变变量的值!)

What is the behavior of the pre-increment/decrement operators (++/--) in Python?

(Python中预增/减运算符(++ /-)的行为是什么?)

Why does Python deviate from the behavior of these operators seen in C/C++?

(为什么Python会偏离C / C ++中看到的这些运算符的行为?)

  ask by Ashwin Nanjappa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

++ 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:

(我不知道Guido van Rossum做出决定的确切论据,但我可以想象一些论点:)

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

    (从技术上讲,解析++count是模棱两可的,因为它可能是++count (两个一元+运算符),就像它可能是++count (一个一元++运算符)一样容易。)

    It's not a significant syntactic ambiguity, but it does exist.

    (它不是语法上的明显歧义,但确实存在。)

  • Simpler language.

    (语言更简单。)

    ++ is nothing more than a synonym for += 1 .

    (++只是+= 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.

    (这是一种速记的发明,因为C编译器很愚蠢,并且不知道如何将a += 1优化a += 1大多数计算机拥有的inc指令。)

    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.

    (在优化编译器和字节码解释语言的这一天,通常不赞成在一种语言中添加运算符以允许程序员优化其代码,尤其是在像Python这样设计成一致且易读的语言中。)

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

    (使用++运算符的语言中一个常见的新手错误是混合使用递增/递减运算符之间的差异(优先级和返回值),Python喜欢消除语言“陷阱”。)

    The precedence issues of pre-/post-increment in C are pretty hairy, and incredibly easy to mess up.

    (C中的预增/后增优先级问题非常棘手,难以置信。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...