我id最近在摆弄并意识到(c?)Python做了一些非常明智的事情:它确保小的int总是有相同的id.
>>> a, b, c, d, e = 1, 2, 3, 4, 5
>>> f, g, h, i, j = 1, 2, 3, 4, 5
>>> [id(x) == id(y) for x, y in zip([a, b, c, d, e], [f, g, h, i, j])]
[True, True, True, True, True]
Run Code Online (Sandbox Code Playgroud)
但后来我想知道数学运算的结果是否也是如此.原来它是:
>>> nines = [(x + y, 9) for x, y in enumerate(reversed(range(10)))]
>>> [id(x) == id(y) for x, y in nines]
[True, True, True, True, True, True, …Run Code Online (Sandbox Code Playgroud)