Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3';s input()返回的str与同一str文本不是同一对象_Python_String - Fatal编程技术网

Python 3';s input()返回的str与同一str文本不是同一对象

Python 3';s input()返回的str与同一str文本不是同一对象,python,string,Python,String,我在Python3中遇到了一个有趣的行为,我不理解。我了解到,对于内置的不可变类型,如str、int等,不仅两个值相同的变量(都包含“x”)相等,而且它们实际上是同一个对象,这允许使用is操作符。但是,当我使用input()函数时,它似乎创建了一个字符串对象,它不是同一个对象,而是具有相同的值 下面是我的python交互式提示: $ python Python 3.2 (r32:88452, Feb 20 2011, 11:12:31) [GCC 4.2.1 (Apple Inc. build

我在Python3中遇到了一个有趣的行为,我不理解。我了解到,对于内置的不可变类型,如str、int等,不仅两个值相同的变量(都包含“x”)相等,而且它们实际上是同一个对象,这允许使用
is
操作符。但是,当我使用input()函数时,它似乎创建了一个字符串对象,它不是同一个对象,而是具有相同的值

下面是我的python交互式提示:

$ python
Python 3.2 (r32:88452, Feb 20 2011, 11:12:31) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input()
test
>>> y = 'test'
>>> x is y
False
>>> x == y
True
>>> id(x)
4301225744
>>> id(y)
4301225576

为什么会这样?

这是一种正常的行为

x == y #True because they have a the same value

x is y #False because x isn't reference to y
id(x) == id(y) #False because as the above
但是:

我了解到,对于内置的不可变类型,如str、int等,不仅两个值相同的变量(都包含“x”)相等,而且它们实际上是同一个对象,这允许使用is运算符

这是您的误解:关于
int
s和
long
s,这只对少数值有效;对于任何类型的字符串,关于一个模块的字符串可能是正确的,但在其他方面则不是


但是,哪一个可以插入任何给定的字符串。

相同的值不能保证是相同的实例。这只是一个你不能依赖的优化。使用
is
合适的次数不多,请改用
=

这是因为一个实现细节-通常不能依赖
is
返回
True
。请尝试以下脚本:

x = 'test'
y = 'test'
print('%r: \'x == y\' is %s, \'x is y\' is %s' % (x, x == y, x is y))
x = 'testtest'
y = 'testtest'
print('%r: \'x == y\' is %s, \'x is y\' is %s' % (x, x == y, x is y))
for i in range(1, 100):
    x = 'test' * i
    y = 'test' * i
    print('%d: %r: \'x == y\' is %s, \'x is y\' is %s' % (i, x, x == y, x is y))
    if x is not y:
        break
这张照片

'test': 'x == y' is True, 'x is y' is True
'testtest': 'x == y' is True, 'x is y' is True
1: 'test': 'x == y' is True, 'x is y' is True
2: 'testtest': 'x == y' is True, 'x is y' is False
在Jython上,
is
即使在第一次打印时也返回
False

我知道,对于内置的不可变类型,比如str、int等,它们不仅是两个相同值的变量(都包含“x”)相等,而且实际上是同一个对象


不,这是不确定的,同样的,如果我说的是一个在物理上与你拿的橘子无法区分的橘子,也不能保证我脑子里没有其他看起来完全相同的橘子。

这只是一个有根据的猜测,但我认为字符串文字是内部的,但是input()的返回值不是。您可能可以使用x=“”.join('test')执行相同的操作,因为它将创建一个新的字符串对象,而不是返回插入的字符串。顺便说一句,这不是3.x所特有的。@Bwmat这应该是一个答案,也是我唯一会接受的答案,因为下面所有的答案都是OP已经知道的(一般来说,
is
=
不同)。不,不是,但input()函数是。我的问题是3中的input()函数,它是3之前的原始输入。我理解为什么equal运算符工作,而is运算符不工作。我不理解的是为什么使用这个不可变类型(
str
),它创建了一个新对象,而不是让它们成为同一个文本对象的典型python行为。也许我只是误解了发生的事情。是不是文本总是同一个对象,但如果它们以不同的方式创建,可能会有具有相同值的不同对象?因为为了“让它们成为同一个文本对象”对象",Python必须以某种方式跟踪第一个对象才能重用它。这样处理文本是可能的,因为它们是在编译脚本时创建的,但仍然不能保证;例如,尝试在两个不同的模块中设置相同的文本。谢谢。有两个很好的答案几乎都是正确的你看到我对内置不可变类型的误解,就直接回答了。
'test': 'x == y' is True, 'x is y' is True
'testtest': 'x == y' is True, 'x is y' is True
1: 'test': 'x == y' is True, 'x is y' is True
2: 'testtest': 'x == y' is True, 'x is y' is False