python使用和pycharm一样吗_Python:使用PyCharm和IDLE / python时的结果不同

I was just reading about the 'unexpected result of is operator' which happens because Python cache numbers between -5 and 256.

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).

When using Python IDLE this is the result:

a = 1000

b = 1000

print (a is b) # prints False

when using Pycharm 5.0.4 this is the result:

a = 1000

b = 1000

print (a is b) # prints True

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.

Edit:

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).

Thanks,

Alon.

解决方案

This is because of how LOAD_CONST byte code works:

Pushes co_consts[consti] onto the stack.

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","","exec")

>>> code2 = compile("a = 1000","","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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值