为什么Python中没有++和——运算符?


当前回答

这并不是因为它没有意义;将“x++”定义为“x+ = 1,求值到x的前一个绑定”非常有意义。

如果你想知道最初的原因,你必须在旧的Python邮件列表中寻找,或者询问当时在那里的人(例如。Guido),但事后很容易就能证明:

简单的递增和递减不需要像其他语言那样多。你不会写这样的for(int i = 0;I < 10;i)在Python中经常使用;相反,你可以把I放在(0,10)范围内。

由于不经常需要它,所以没有理由给它自己的特殊语法;当您确实需要增加时,+=通常就可以了。

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 four operators--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 -=).

这些都是+=和-=的冗余,所以会变成净损失。

其他回答

我对python为什么没有++操作符的理解如下:当你用python写这个时,a=b=c=1,你会得到三个变量(标签)指向同一个对象(值为1)。你可以使用id函数来验证这一点,它将返回一个对象内存地址:

In [19]: id(a)
Out[19]: 34019256

In [20]: id(b)
Out[20]: 34019256

In [21]: id(c)
Out[21]: 34019256

所有三个变量(标签)都指向同一个对象。现在增加变量之一,看看它是如何影响内存地址的:

In [22] a = a + 1

In [23]: id(a)
Out[23]: 34019232

In [24]: id(b)
Out[24]: 34019256

In [25]: id(c)
Out[25]: 34019256

你可以看到变量a现在指向另一个对象,变量b和c。因为你已经使用了a = a + 1,这是明确的。换句话说,你将完全另一个对象赋值给标签a。想象一下,你可以写一个++,这将表明你没有给变量赋值一个新对象,而是给旧对象赋值。所有这些东西都是为了尽量减少混淆。为了更好地理解python变量是如何工作的:

在Python中,为什么函数可以修改调用者感知到的一些参数,而不能修改其他参数?

Python是按值调用还是按引用调用?既不。

Python是按值传递还是按引用传递?

Python是按引用传递还是按值传递?

如何通过引用传递变量?

理解Python变量和内存管理

在python中模拟值传递行为

Python函数通过引用调用

像Pythonista一样编写代码:地道的Python

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.

这并不是因为它没有意义;将“x++”定义为“x+ = 1,求值到x的前一个绑定”非常有意义。

如果你想知道最初的原因,你必须在旧的Python邮件列表中寻找,或者询问当时在那里的人(例如。Guido),但事后很容易就能证明:

简单的递增和递减不需要像其他语言那样多。你不会写这样的for(int i = 0;I < 10;i)在Python中经常使用;相反,你可以把I放在(0,10)范围内。

由于不经常需要它,所以没有理由给它自己的特殊语法;当您确实需要增加时,+=通常就可以了。

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 four operators--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 -=).

这些都是+=和-=的冗余,所以会变成净损失。

正如我理解的那样,你不会认为内存中的值被改变了。 在c语言中,当你执行x++时,内存中的x值会发生变化。 但在python中,所有数字都是不可变的,因此x指向的地址仍然是x而不是x+1。当你写x++时,你会认为x改变了,实际上发生的是x引用被改变到内存中存储x+1的位置,或者如果doe不存在,重新创建这个位置。

因为,在Python中,整数是不可变的(int's +=实际上返回一个不同的对象)。

同样,使用++/——,您需要考虑前后的递增/递减,并且只需要多击一次键就可以写出x+=1。换句话说,它以很少的收益为代价避免了潜在的混乱。