相关疑难解决方法(0)

为什么(0-6)是-6 =假?

可能重复:
Python"is"运算符使用整数意外运行

今天我试着调试我的项目,经过几个小时的分析,我得到了这个:

>>> (0-6) is -6
False
Run Code Online (Sandbox Code Playgroud)

但,

>>> (0-5) is -5
True
Run Code Online (Sandbox Code Playgroud)

你能解释一下,为什么?也许这是某种错误或非常奇怪的行为.

> Python 2.7.3 (default, Apr 24 2012, 00:00:54) [GCC 4.7.0 20120414 (prerelease)] on linux2
>>> type(0-6) 
<type 'int'>
>>> type(-6) 
<type 'int'>
>>> type((0-6) is -6)
<type 'bool'>
>>> 
Run Code Online (Sandbox Code Playgroud)

python debugging integer cpython

129
推荐指数
4
解决办法
4897
查看次数

比较字符串和空格时,'is'运算符的行为不同

我开始学习Python(python 3.3),我正在尝试is运算符.我试过这个:

>>> b = 'is it the space?'
>>> a = 'is it the space?'
>>> a is b
False
>>> c = 'isitthespace'
>>> d = 'isitthespace'
>>> c is d
True
>>> e = 'isitthespace?'
>>> f = 'isitthespace?'
>>> e is f
False
Run Code Online (Sandbox Code Playgroud)

似乎空间和问号使is行为表现不同.这是怎么回事?

编辑:我知道我应该使用==,我只是想知道为什么is会这样.

python operators object-identity python-3.x

29
推荐指数
3
解决办法
1908
查看次数