本文翻译自:Python integer incrementing with ++ [duplicate]
Possible Duplicate: 可能重复:
Python: Behaviour of increment and decrement operators Python:增量和减量运算符的行为
I've always laughed to myself when I've looked back at my VB6 days and thought, "What modern language doesn't allow incrementing with double plus signs?": 当我回顾我的VB6时,我总是嘲笑自己,并且想:“现代语言不允许用双加号增加?”:
number++
To my surprise, I can't find anything about this in the Python docs. 令我惊讶的是,我在Python文档中找不到任何相关内容。 Must I really subject myself to number = number + 1
? 我真的必须让自己受到number = number + 1
吗? Don't people use the ++/--
notation? 人们不使用++/--
符号吗?
#1楼
参考:https://stackoom.com/question/B2sX/Python整数递增与-重复
#2楼
Python不支持++
,但您可以这样做:
number += 1
#3楼
Yes. 是。 The ++
operator is not available in Python. 该++
运算符是不可用Python编写的。 Guido doesn't like these operators. Guido不喜欢这些运营商。
#4楼
你可以做:
number += 1
#5楼
您可以使用:
number += 1
#6楼
Take a look at Behaviour of increment and decrement operators in Python for an explanation of why this doesn't work. 看看Python中增量和减量运算符的行为,以解释为什么这不起作用。
Python doesn't really have ++ and --, and I personally never felt it was such a loss. Python并没有真正拥有++和 - 而且我个人从未觉得这是一种损失。
I prefer functions with clear names to operators with non-always clear semantics (hence the classic interview question about ++x vs. x++ and the difficulties of overloading it). 我更喜欢具有明确名称的函数到具有非始终清晰语义的运算符(因此关于++ x与x ++的经典访谈问题以及重载它的困难)。 I've also never been a huge fan of what post-incrementation does for readability. 我也从未成为后增量对可读性的忠实粉丝。
You could always define some wrapper class (like accumulator) with clear increment semantics, and then do something like x.increment() or x.incrementAndReturnPrev() 您总是可以使用明确的增量语义定义一些包装类(如累加器),然后执行类似x.increment()或x.incrementAndReturnPrev()的操作。