为什么Python中没有++和-运算符? [英] Why are there no ++ and --​ operators in Python?

查看:59
本文介绍了为什么Python中没有++和-运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why are there no ++ and -- operators in Python?

推荐答案

不是因为它没有意义,而是因为它没有意义.最好将"x ++"定义为"x + = 1,求出x的先前绑定".

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

如果您想知道最初的原因,则必须浏览旧的Python邮件列表或询问那里的某个人(例如Guido),但这很容易找到事实依据:

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中编写类似for(int i = 0; i < 10; ++i)之类的东西.相反,您可以执行for i in range(0, 10)之类的事情.

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

由于几乎不需要它,因此没有太多理由使用它自己的特殊语法;当您确实需要增加时,通常+=就可以了.

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.

这不是一个有意义的决定,还是一个是否可以做到的决定,它确实可以做到.这是一个好处是否值得添加到该语言的核心语法中的问题.请记住,这是四个运算符-postinc,postdec,preinc,predec,并且每个运算符都需要具有自己的类重载;它们都需要指定和测试;它将在语言中添加操作码(暗示更大,因此更慢的VM引擎);每个支持逻辑增量的类都需要实现它们(在+=-=之上).

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

这对于+=-=都是多余的,因此将成为净亏损.

This is all redundant with += and -=, so it would become a net loss.

这篇关于为什么Python中没有++和-运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆