问题:请找出一个数字,满足下面检查函数
- def check(x):
- if x+1 is 1+x:
- return False
- if x+2 is not 2+x:
- return False
- return True
-
- <function check at 0x7f10bfd97b18>
-
答案是 -7,原因是在 Python 中, -5 到 256 的整数是预先分配好的,任何计算结果在这个范围内的数字,都是直接返回该单例,因此 is 操作为真。
- print(check(-6))
- print(check(-7))
- print(check(-8))
-
- False
- True
- False