Python:使用PyCharm和IDLE / python时的结果不同 [英] Python: Different results when using PyCharm and IDLE/python
问题描述
我刚刚读到了'运算符'的意外结果,因为Python缓存数介于-5到256之间。
I was just reading about the 'unexpected result of is operator' which happens because Python cache numbers between -5 and 256.
这里讨论了这个问题:
is运算符使用整数意外行为
This was discussed here: "is" operator behaves unexpectedly with integers
此处:
"是"和id在Python 3.5中
当我运行其中一个示例时,我在Python Idle和Python IDE之间得到了不同的结果(我正在使用Jetbrains Pycharm专业版)版本 - 5.0.4)。
When I run one of the examples given there, I get different results between Python Idle and Python IDE (I'm using Jetbrains Pycharm professional edition - 5.0.4).
使用Python IDLE时,结果如下:
When using Python IDLE this is the result:
a = 1000
b = 1000
print (a is b) # prints False
使用Pycharm 5.0.4时结果如下:
when using Pycharm 5.0.4 this is the result:
a = 1000
b = 1000
print (a is b) # prints True
这怎么可能?
我已经重新检查了,我的项目的Python-Interpreter在两种情况下完全相同(都是Python 3.5.1)。
不确定这是否是我做错了,我希望有人能解释一下。
how could this be? I've rechecked, and my project's Python-Interpreter is exactly the same in both cases (both are Python 3.5.1). Not sure if this is something I've done wrong, and I was hoping if someone could explain this.
编辑:
我知道'a'是'b'==如果id(a)== id(b),你可以检查它,就像你提到的一些人一样在评论中。也许我应该更清楚,我不明白的是IDE怎么会有不同的行为?我想(并请,纠正我,因为我似乎错了)IDE只是一个使用外部编译器/解释器的用户友好环境,这就是为什么它们独立于那些IDE(例如,pycharm支持)不仅是Python,而且我可以使用C编译器或Java等运行Eclipse(所有这些都不是IDE的一部分)。
I know 'a' is 'b' == true iff id(a) == id(b), and that you can check it like some of you mentioned in the comments. Perhaps I should have been more clear, what I don't understand is how could it be that an IDE has different behavior? I thought (and please, correct me, as it seems I'm wrong) that an IDE is just a user-friendly environment that uses external compilers / interpreters, and this is why these are independent of those IDE's (for instance, pycharm supports not only Python, and I could run Eclipse with C compiler, or Java etc. (all of which are not parts of the IDE).
谢谢,
Alon。
Thanks, Alon.
推荐答案
这是因为 LOAD_CONST
字节代码有效:
This is because of how LOAD_CONST
byte code works:
将
co_consts [consti]
推入堆栈。
由于整数存储为常量,因此在同一上下文中对同一整数的赋值将产生完全相同的结果,我们可以看到 LOAD_CONST
的争论是对于a和b, 0
:
Since integers are stored as constants then assignments to the same integer in the same context will yield the exact same result, we can see that the arguement to LOAD_CONST
is 0
for both a and b:
>>> import dis
>>> dis.dis("a = 1000 ; b = 1000")
1 0 LOAD_CONST 0 (1000)
3 STORE_NAME 0 (a)
6 LOAD_CONST 0 (1000)
9 STORE_NAME 1 (b)
12 LOAD_CONST 1 (None)
15 RETURN_VALUE
# ^ this is the argument
在交互式会话中,每个命令都是单独编译的(这样它们可以单独执行)所以常量会有所不同:
where as in an interactive session each command is compiled separately (so that they can be executed separately) so the constants will be different:
>>> code1 = compile("a = 1000","<dummy file>","exec")
>>> code2 = compile("a = 1000","<dummy file>","exec")
>>> code1.co_consts, code2.co_consts
((1000, None), (1000, None))
>>> code1.co_consts[0] is code2.co_consts[0]
False
同样的常数函数中的函数总是相同但它与其他函数中的常量不同:
Similarly the constant in a function will always be the same but it will be different to the constant in other functions:
def f():
return 1000
def g():
return 1000 #different code object!!
#these all work
assert f() is f()
assert g() is g()
assert f() is not g()
assert f() is not 1000 and g() is not 1000
另请注意, @AniMenon 已经指出从-5到256的数字是用于优化的单例,所以对于数字中的数字不一样那个范围。
Also note that as @AniMenon has pointed out the numbers from -5 to 256 are singletons for optimization so the same will not hold true for numbers in that range.
这篇关于Python:使用PyCharm和IDLE / python时的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!