为什么没有++和--?Python中的运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3654830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are there no ++ and --? operators in Python?
提问by Leonid
Why are there no ++and --operators in Python?
为什么Python 中没有++and--运算符?
采纳答案by Glenn Maynard
It's not because it doesn't make sense; it makes perfect sense to define "x++" as "x += 1, evaluating to the previous binding of x".
这不是因为它没有意义;将“x++”定义为“x += 1,计算x 的先前绑定”是完全合理的。
If you want to know the original reason, you'll have to either wade through old Python mailing lists or ask somebody who was there (eg. Guido), but it's easy enough to justify after the fact:
如果你想知道最初的原因,你要么翻阅旧的 Python 邮件列表,要么询问当时在场的人(例如 Guido),但事后证明很容易:
Simple increment and decrement aren't needed as much as in other languages. You don't write things like for(int i = 0; i < 10; ++i)in Python very often; instead you do things like for i in range(0, 10).
不像其他语言那样需要简单的递增和递减。你不会for(int i = 0; i < 10; ++i)经常用 Python写东西;相反,你会做类似的事情for i in range(0, 10)。
Since it's not needed nearly as often, there's much less reason to give it its own special syntax; when you do need to increment, +=is usually just fine.
因为它几乎不需要经常使用,所以没有理由给它自己的特殊语法;当您确实需要增加时,+=通常就可以了。
It's not a decision of whether it makes sense, or whether it can be done--it does, and it can. It's a question of whether the benefit is worth adding to the core syntax of the language. Remember, this is fouroperators--postinc, postdec, preinc, predec, and each of these would need to have its own class overloads; they all need to be specified, and tested; it would add opcodes to the language (implying a larger, and therefore slower, VM engine); every class that supports a logical increment would need to implement them (on top of +=and -=).
这不是决定它是否有意义,或者它是否可以完成——它确实,而且它可以。这是一个问题,该好处是否值得添加到该语言的核心语法中。请记住,这是四个操作符——postinc、postdec、preinc、predec,并且每个操作符都需要有自己的类重载;它们都需要被指定和测试;它将向语言添加操作码(意味着更大,因此更慢的 VM 引擎);每个支持逻辑增量的类都需要实现它们(在+=和之上-=)。
This is all redundant with +=and -=, so it would become a net loss.
这与+=和都是多余的-=,因此它将成为净损失。
回答by Reed Copsey
It was just designed that way. Increment and decrement operators are just shortcuts for x = x + 1. Python has typically adopted a design strategy which reduces the number of alternative means of performing an operation. Augmented assignmentis the closest thing to increment/decrement operators in Python, and they weren't even added until Python 2.0.
它就是这样设计的。自增和自减运算符只是x = x + 1. Python 通常采用一种设计策略,以减少执行操作的替代方法的数量。 增广赋值是 Python 中最接近自增/自减运算符的东西,它们甚至直到 Python 2.0 才被添加。
回答by msw
This original answer I wrote is a myth from the folklore of computing: debunked by Dennis Ritchie as "historically impossible" as noted in the letters to the editors of Communications of the ACMJuly 2012 doi:10.1145/2209249.2209251
我写的这个原始答案是计算民间传说中的一个神话:丹尼斯·里奇 (Dennis Ritchie) 在给ACM 通讯编辑2012 年 7 月doi:10.1145/2209249.2209251的信中指出“历史上不可能”
The C increment/decrement operators were invented at a time when the C compiler wasn't very smart and the authors wanted to be able to specify the direct intent that a machine language operator should be used which saved a handful of cycles for a compiler which might do a
C 递增/递减运算符是在 C 编译器不是很聪明的时候发明的,作者希望能够指定应该使用机器语言运算符的直接意图,这为编译器节省了一些周期可能会做一个
load memory
load 1
add
store memory
instead of
代替
inc memory
and the PDP-11 even supported "autoincrement" and "autoincrement deferred" instructions corresponding to *++pand *p++, respectively. See section 5.3 of the manualif horribly curious.
并且 PDP-11 甚至支持分别对应于*++p和 的“自动增量”和“自动增量延迟”指令*p++。如果非常好奇,请参阅手册的第 5.3 节。
As compilers are smart enough to handle the high-level optimization tricks built into the syntax of C, they are just a syntactic convenience now.
由于编译器足够聪明,可以处理内置于 C 语法中的高级优化技巧,它们现在只是一种语法上的便利。
Python doesn't have tricks to convey intentions to the assembler because it doesn't use one.
Python 没有向汇编程序传达意图的技巧,因为它不使用。
回答by mkoistinen
I'm very new to python but I suspect the reason is because of the emphasis between mutable and immutable objects within the language. Now, I know that x++ can easily be interpreted as x = x + 1, but it LOOKS like you're incrementing in-placean object which could be immutable.
我对 python 很陌生,但我怀疑原因是因为语言中可变和不可变对象之间的重点。现在,我知道 x++ 可以很容易地解释为 x = x + 1,但看起来您正在就地递增一个可能不可变的对象。
Just my guess/feeling/hunch.
只是我的猜测/感觉/预感。
回答by Ludovico Fischer
Maybe a better question would be to ask why do these operators exist in C. K&R calls increment and decrement operators 'unusual' (Section 2.8page 46). The Introduction calls them 'more concise and often more efficient'. I suspect that the fact that these operations always come up in pointer manipulation also has played a part in their introduction. In Python it has been probably decided that it made no sense to try to optimise increments (in fact I just did a test in C, and it seems that the gcc-generated assembly uses addl instead of incl in both cases) and there is no pointer arithmetic; so it would have been just One More Way to Do It and we know Python loathes that.
也许更好的问题是问为什么 C 中存在这些运算符。 K&R 调用增量和减量运算符“不寻常”(第 2.8 页第 46 节)。引言称它们“更简洁,通常更有效”。我怀疑这些操作总是出现在指针操作中的事实也在它们的介绍中发挥了作用。在 Python 中,可能已经确定尝试优化增量是没有意义的(实际上我只是在 C 中做了一个测试,似乎 gcc 生成的程序集在这两种情况下都使用 addl 而不是 incl)并且没有指针运算;所以这只是另一种方式,我们知道 Python 讨厌这样做。
回答by Nathan Davis
Because, in Python, integers are immutable (int's += actually returns a different object).
因为,在 Python 中,整数是不可变的(int 的 += 实际上返回一个不同的对象)。
Also, with ++/-- you need to worry about pre- versus post- increment/decrement, and it takes only one more keystroke to write x+=1. In other words, it avoids potential confusion at the expense of very little gain.
此外,使用 ++/-- 您需要担心前后递增/递减,并且只需再敲一次键即可写入x+=1。换句话说,它以非常小的增益为代价避免了潜在的混淆。
回答by EMP
Of course, we could say "Guido just decided that way", but I think the question is really about the reasons for that decision. I think there are several reasons:
当然,我们可以说“Guido 就是这样决定的”,但我认为问题实际上是关于那个决定的原因。我认为有几个原因:
- It mixes together statements and expressions, which is not good practice. See http://norvig.com/python-iaq.html
- It generally encourages people to write less readable code
- Extra complexity in the language implementation, which is unnecessary in Python, as already mentioned
- 它将语句和表达式混合在一起,这不是好的做法。见http://norvig.com/python-iaq.html
- 它通常鼓励人们编写可读性较差的代码
- 如前所述,语言实现的额外复杂性,这在 Python 中是不必要的
回答by Sepheus
I believe it stems from the Python creed that "explicit is better than implicit".
我相信它源于 Python 信条“显式优于隐式”。
回答by GSto
I always assumed it had to do with this line of the zen of python:
我一直认为这与 python 的禅宗这一行有关:
There should be one — and preferably only one — obvious way to do it.
应该有一种——最好只有一种——明显的方法来做到这一点。
x++ and x+=1 do the exact same thing, so there is no reason to have both.
x++ 和 x+=1 做完全相同的事情,所以没有理由同时拥有。
回答by wberry
First, Python is only indirectly influenced by C; it is heavily influenced by ABC, which apparently does not have these operators, so it should not be any great surprise not to find them in Python either.
首先,Python 只是间接受到 C 的影响;它深受ABC 的影响,显然它没有这些操作符,所以在 Python 中找不到它们也不应该是什么大惊喜。
Secondly, as others have said, increment and decrement are supported by +=and -=already.
其次,正如其他人所说,递增和递减由支持+=和-=了。
Third, full support for a ++and --operator set usually includes supporting both the prefix and postfix versions of them. In C and C++, this can lead to all kinds of "lovely" constructs that seem (to me) to be against the spirit of simplicity and straight-forwardness that Python embraces.
第三,对++and--运算符集的完全支持通常包括支持它们的前缀和后缀版本。在 C 和 C++ 中,这可能会导致各种“可爱”的构造,这些构造(在我看来)与 Python 所拥抱的简单和直接的精神背道而驰。
For example, while the C statement while(*t++ = *s++);may seem simple and elegant to an experienced programmer, to someone learning it, it is anything but simple. Throw in a mixture of prefix and postfix increments and decrements, and even many pros will have to stop and think a bit.
例如,虽然 C 语句while(*t++ = *s++);对于有经验的程序员来说可能看起来简单而优雅,但对于学习它的人来说,它并不简单。加上前缀和后缀增量和减量的混合,甚至许多专业人士也不得不停下来思考一下。

