As any Python programmer knows, you should use ==
instead of is
to compare two strings for equality. However, are there actually any cases where ( s is "" )
and ( s == "" )
will give different results in Python 2.6.2?
I recently came across code that used ( s is "" )
in code review, and while pointing out that this was incorrect I wanted to give an example of how this could fail. But try as I might, I can't construct two empty strings with different identities. It seems that the Python implementation must special-case the empty string in lots of common operations. For example:
>>> a = ""
>>> b = "abc"[ 2:2 ]
>>> c = ''.join( [] )
>>> d = re.match( '()', 'abc' ).group( 1 )
>>> e = a + b + c + d
>>> a is b is c is d is e
True
However, this question suggests that there are cases where ( s is "" )
and ( s == "" )
can be different. Can anyone give me an example?