This is my first post here, I hope i'm not wrong to do so ;-)
(sorry for my bad English language, I am German)
I've read a lot about the '==' operator in python in many different posts and I also know that python make for strings 'string interning'
Wikipedia about string interning
It is clear to me that the '==' operator and the 'is' operator output different things if you use the operators for comparison of strings.
The '==' operator compares the content of the both strings character by character, the 'is' operator compares the memory address from the objects and it is not sure that equality is recognized self if both strings are in the objects the same because 'string interning' not always happen at 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"
That produces this output:
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
So all of this is clear for me as i have read from many places.
NOW MY QUESTION (I Have yet to find an answer)
How does the '==' operator in python INTERNAL really work when comparing two strings, also I think because 'string interning' must work the '==' operator internal so that 'string interning' make sense (demonstrated with pseudo code):
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
IS THIS CORRECT?
UPDATE BY ME: Many thanks for the help (and for the fixing of my brocken english language), and thanks for the remark that my question was an duplicate and the linking on Location of python string class in the source code!
Because this help I found out that it is really so how I had thought
Functionality is for strings implemented in function 'string_richcompare' in https://svn.python.org/projects/python/trunk/Objects/stringobject.c and it is for unicodes implemented in function 'PyUnicode_Compare' in https://svn.python.org/projects/python/trunk/Objects/unicodeobject.c
Also very good information sources round about how python internal handle strings are to find here: http://guilload.com/python-string-interning/
https://www.laurentluce.com/posts/python-string-objects-implementation/