python中的==运算符INTERNAL如何在字符串比较中真正起作用

时间:2018-03-21 15:52:59

标签: python

这是我在这里发表的第一篇文章,我希望我这样做没有错; - )

(抱歉我的英语不好,我是德国人)

我已经阅读了很多关于' ==' python中的操作符在许多不同的帖子中我也知道python为字符串制作字符串实习'

Wikipedia about string interning

Stackoverflow task

On thepythonguru.com

我很清楚,' =='运营商和'是'如果使用运算符来比较字符串,则运算符输出不同的内容。

' =='运算符逐个字符地比较两个字符串的内容,'是'运算符比较对象的内存地址,如果两个字符串在对象中相同,则不确定是否会识别相等性,因为' string interning'并不总是发生在python。

str1 = "a"
str2 = "a"

print "String interning is happen"
print "ID from str1 " + str(id(str1))
print "ID from str2 " + str(id(str2))

# Detect sure that the content is the same"
if str1 == str2:
    print "Equal"
else:
    print "Not equal"

# Detect in this case correct the equality because "String interning" is happen
# (make internal id(str1) == id(str2) and that get an POSITIVE result)
if str1 is str2:
    print "Equal"
else:
    print "Not equal"

str3 = "bb"
str4 = "".join(["b", "b"])

print "String interning is NOT happen"
print "ID from str3 " + str(id(str3))
print "ID from str4 " + str(id(str4))

# Detect sure that the content is the same:
if str3 == str4:
    print "Equal"
else:
    print "Not equal"

# Detect in this case NOT the equality because "String interning" is NOT happen
# (make internal id(str1) == id(str2) and that get an NEGATIVE result)
if str3 is str4:
    print "Equal"
else:
    print "Not equal"

产生此输出:

String interning is happen
ID from str1 32170856
ID from str2 32170856
Equal
Equal
String interning is NOT happen
ID from str3 41567752
ID from str4 41090272
Equal
Not equal

所以这一切对我来说都是清楚的,因为我从许多地方读过。

现在我的问题(我还没有找到答案)

' ==' python INTERNAL中的运算符在比较两个字符串时确实有用,我想也是因为'字符串实习'必须工作' =='操作员内部,以便'字符串实习'有意义(用伪代码演示):

def equal_operator_at_strings(string_a, string_b):
    # Go very fast because only two pointers are compared:
    if id(string_a) == id(string_b):
        # If 'string interning' was happen in the past so we have stored an lot of time
        return True

    # Go also very fast:
    if len(string_a) != len(string_b):
        # If the strings hav NOT the same length so are the strings NOT equal
        return False

    # If this here is reached so have python detected that the objects, which
    # represent both an string, have not the same ID ('string interning' was NOT
    # happen in the past), but the content of the both objects can be the same
    # (because both strings have the same length) therefore is now an
    # comparision need sign by sign so that we can sure say are the strings
    # equal are not, dependent of the string lengths can this now need an
    # lot of time 

这是正确的吗?

我更新: 非常感谢帮助(以及修复我的brocken英语),并感谢我的问题是重复和Location of python string class in the source code上的链接!

因为这个帮助我发现我的想法真是如此

功能适用于在函数' string_richcompare'中实现的字符串。在https://svn.python.org/projects/python/trunk/Objects/stringobject.c 它是用于在函数' PyUnicode_Compare'中实现的unicodes。在 https://svn.python.org/projects/python/trunk/Objects/unicodeobject.c

关于如何在这里找到python内部句柄字符串的非常好的信息来源: http://guilload.com/python-string-interning/

https://www.laurentluce.com/posts/python-string-objects-implementation/

0 个答案:

没有答案