比较字符串和空格时,"is"运算符的行为有所不同 [英] 'is' operator behaves differently when comparing strings with spaces

查看:126
本文介绍了比较字符串和空格时,"is"运算符的行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习Python(python 3.3),并且正在尝试is运算符.我试过了:

I've started learning Python (python 3.3) and I was trying out the is operator. I tried this:

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

似乎空格和问号使is的行为有所不同.发生了什么事?

It seems like the space and the question mark make the is behave differently. What's going on?

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

I know I should be using ==, I just wanted to know why is behaves like this.

推荐答案

警告:这个答案是关于特定python解释器的实现细节的.将字符串与is ==坏主意进行比较.

Warning: this answer is about the implementation details of a specific python interpreter. comparing strings with is==bad idea.

好吧,至少对于cpython3.4/2.7.3,答案是不,不是空格".不是空白:

Well, at least for cpython3.4/2.7.3, the answer is "no, it is not the whitespace". Not only the whitespace:

  • 如果两个字符串文字是字母数字或驻留在同一 block (文件,函数,类或单个解释器命令)中,则它们将共享内存

  • Two string literals will share memory if they are either alphanumeric or reside on the same block (file, function, class or single interpreter command)

一个计算结果为字符串的表达式将导致一个对象与使用字符串文字创建的对象相同,前提是且仅当使用常量和二进制/一元运算符创建该对象时,且所得字符串为少于21个字符.

An expression that evaluates to a string will result in an object that is identical to the one created using a string literal, if and only if it is created using constants and binary/unary operators, and the resulting string is shorter than 21 characters.

单个字符是唯一的.

字母数字字符串文字始终共享内存:

Alphanumeric string literals always share memory:

>>> x='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
>>> y='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
>>> x is y
True

当且仅当非字母数字字符串文字共享封闭的语法块时,它们才会共享内存:

Non-alphanumeric string literals share memory if and only if they share the enclosing syntactic block:

(解释器)

>>> x='`!@#$%^&*() \][=-. >:"?<a'; y='`!@#$%^&*() \][=-. >:"?<a';
>>> z='`!@#$%^&*() \][=-. >:"?<a';
>>> x is y
True 
>>> x is z
False 

(文件)

x='`!@#$%^&*() \][=-. >:"?<a';
y='`!@#$%^&*() \][=-. >:"?<a';
z=(lambda : '`!@#$%^&*() \][=-. >:"?<a')()
print(x is y)
print(x is z)

输出:TrueFalse

对于简单的二进制操作,编译器将进行非常简单的常量传播(请参见 peephole.c ),但只有在结果字符串小于21个字符的情况下,它才会这样做.在这种情况下,前面提到的规则将生效:

For simple binary operations, the compiler is doing very simple constant propagation (see peephole.c), but with strings it does so only if the resulting string is shorter than 21 charcters. If this is the case, the rules mentioned earlier are in force:

>>> 'a'*10+'a'*10 is 'a'*20
True
>>> 'a'*21 is 'a'*21
False
>>> 'aaaaaaaaaaaaaaaaaaaaa' is 'aaaaaaaa' + 'aaaaaaaaaaaaa'
False
>>> t=2; 'a'*t is 'aa'
False
>>> 'a'.__add__('a') is 'aa'
False
>>> x='a' ; x+='a'; x is 'aa'
False

单个字符总是共享内存,当然:

Single characters always share memory, of course:

>>> chr(0x20) is ' '
True

这篇关于比较字符串和空格时,"is"运算符的行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆