小さな整数をキャッシュするPythonに関するこの質問に触発されました。
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