受到有关Python缓存小整数的这个问题的启发。
Python编译器是否可以在编译时将(0 - 6)替换为-6?下面的代码表明它没有这样做。如果不可能,为什么呢?我认为
如果这是可能的,那么为什么CPython没有这样做呢?
Python编译器是否可以在编译时将(0 - 6)替换为-6?下面的代码表明它没有这样做。如果不可能,为什么呢?我认为
0
,-
或6
的含义在运行时不会有所不同。如果这是可能的,那么为什么CPython没有这样做呢?
# test_integers.py
def test_integers():
print "-6 is -6 ?", -6 is -6 # True
print "(0 - 6) is -6 ?", (0 - 6) is -6 # False
# import_test_integers.py
import test_integers
test_integers.test_integers()
如果这个问题与具体的实现有关,以下是我的Python详细信息:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2