''s'操作在终端和皮查尔

问题描述 投票:3回答:1

我知道'is'运算符与字符串的区别。但是,当我在Pycharm中键入以下示例时,总是得到True

a = 'a monkey'
b = 'a monkey'
print(a is b)

我得到:

True

在终端:

>>> a = 'a monkey'
>>> b = 'a monkey'
>>> a is b
False

为什么会这样?

python terminal pycharm
1个回答
1
投票

这是由于python的内部化,这是一个实现细节,

>>> a = 'monkey'
>>> b = 'monkey'
>>> a is b
True
>>> id(a)
139786601164400
>>> id(b)
139786601164400
>>> c = 'monkey <with some added strings to go over the limit'
>>> d = 'monkey <with some added strings to go over the limit'
>>> c is d
False
>>> id(c)
139786601121472
>>> id(d)
139786601121584
© www.soinside.com 2019 - 2024. All rights reserved.