python - 为什么Python中没有 "++"操作?

标签 python

<分区>

Possible Duplicate:
Why there are no ++ and — operators in Python?

这个问题可能看起来很奇怪,但我想知道为什么Python中没有这样的操作。

我知道,x += 1 几乎和 x++ 一样简单,但仍然如此。在我熟悉的大多数语言(C、C++、Java、C#、JavaScript、PHP)中都有这样的操作,但在 Python 中没有。

也许这与这种语言的哲学有关?

最佳答案

为了避免前置增量、后增量混淆并保持简单。

intlong 等数据类型也是不可变的。 ++--操作符的含义是改变当前的数据类型。因此不支持。

在增量操作之后,python 对象本身发生了变化。

>>> a = int(10)
>>> b = a
>>> print id(a), id(b)
166744132 166744132
>>> a += 1
>>> print id(a), id(b)
166744120 166744132
>>> print a, b
11 10

重复问题如下,其中包含更多信息:

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

Behaviour of increment and decrement operators in Python

关于python - 为什么Python中没有 "++"操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902305/

相关文章:

python - 如果字典的键位于卡住集中,则检索值

python - 用Python处理视频的好资源?

Python __init__(a,b,c,d) 不识别 "self"作为参数

python - 使用 Sympy 在 python 中进行微分

python - 给定数据框中的 pd.Interval 列,过滤落在 Interval 范围内的值

python - Pandas .dropna() 在指定属性上

python - 为什么当键不在字典中时有些语言会返回 nil,而 Python 会抛出异常?

python - 在 python 变量中获取 wget 输出

python - brew install python3 没有安装 pip3

python - 使用 Python/PIL 或类似工具来缩小空格