用“is”比较两个字符串 -- 没有达到预期效果

发布于 2024-07-29 18:59:10 字数 696 浏览 10 评论 0原文

我正在尝试使用 is 比较两个字符串。 一个字符串由函数返回,另一个字符串刚刚在比较中声明。 测试对象身份,但根据此页面,由于 Python 的内存优化,它还可以处理两个相同的字符串。 但是,以下内容不起作用:

def uSplit(ustring):
        #return user minus host
        return ustring.split('!',1)[0]

user = uSplit('theuser!host')
print type(user)
print user
if user is 'theuser':
    print 'ok'
else:
    print 'failed'

user = 'theuser'

if user is 'theuser':
    print 'ok'

输出:

type 'str'
theuser
failed
ok

我猜测其原因是函数返回的字符串是与字符串文字不同的字符串“类型”。 无论如何,有没有一个函数可以返回字符串文字? 我知道我可以使用 ==,但我只是好奇。

I'm attempting to compare two strings with is. One string is returned by a function, and the other is just declared in the comparison. is tests for object identity, but according to this page, it also works with two identical strings because of Python's memory optimization. But, the following doesn't work:

def uSplit(ustring):
        #return user minus host
        return ustring.split('!',1)[0]

user = uSplit('theuser!host')
print type(user)
print user
if user is 'theuser':
    print 'ok'
else:
    print 'failed'

user = 'theuser'

if user is 'theuser':
    print 'ok'

The output:

type 'str'
theuser
failed
ok

I'm guessing the reason for this is a string returned by a function is a different "type" of string than a string literal. Is there anyway to get a function to return a string literal? I know I could use ==, but I'm just curious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-08-05 18:59:10

您引用的那个页面说“如果两个字符串文字相等,则它们已被放入相同的内存位置”(强调我的)。 Python 实习生文字字符串,但从某个任意函数返回的字符串是单独的对象。 is 运算符可以被认为是指针比较,因此两个不同的对象不会被视为相同(即使它们包含相同的字符,即它们相等)。

That page you quoted says "If two string literals are equal, they have been put to same memory location" (emphasis mine). Python interns literal strings, but strings that are returned from some arbitrary function are separate objects. The is operator can be thought of as a pointer comparison, so two different objects will not compare as identical (even if they contain the same characters, ie. they are equal).

夏日落 2024-08-05 18:59:10

你引用的网站是这样说的:

如果两个字符串文字相等,则它们已被放入同一内存位置。

uSplit('theuser!host')

不是字符串文字——它是对文字 'theuser!host' 进行操作的结果。

无论如何,您通常不应该使用 is 检查字符串相等性,因为无论如何,这种内存优化只是您不应该依赖的实现细节。


另外,您应该使用 is 来表示 is None 之类的内容。 使用它来检查您设计的类的两个对象是否是同一个实例。 您不能轻松地将它用于字符串或数字,因为创建这些内置类的规则很复杂。 有些字符串被保留。 同样,有些数字也被拘留。

The site you quote says this:

If two string literals are equal, they have been put to same memory location.

But

uSplit('theuser!host')

is not a string literal -- it's the result of an operation on the literal 'theuser!host'.

Anyway, you usually shouldn't check for string equality using is, because this memory optimization in any case is just an implementation detail you shouldn't rely on.


Also, You should use is for things like is None. Use it for checking to see if two objects -- of classes that you designed -- are the same instance. You can't easily use it for strings or numbers because the rules for creation of those built-in classes are complex. Some strings are interned. Some numbers, similarly, are interned.

相对绾红妆 2024-08-05 18:59:10

您遇到的情况是,Python 并不总是保留其所有字符串。 更多详细信息请参见:

http://mail.python.org/pipermail/导师/2009-July/070157.html

What you have run into is the fact that Python does not always intern all of its strings. More detail here:

http://mail.python.org/pipermail/tutor/2009-July/070157.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文