问题
I noticed the following holds:
>>> u'abc' == 'abc'
True
>>> 'abc' == u'abc'
True
Will this always be true or could it possibly depend on the system locale? (It seems strings are unicode in python 3: e.g. this question, but bytes in 2.x)
回答1:
Python 2 coerces between unicode
and str
using the ASCII codec when comparing the two types. So yes, this is always true.
That is to say, unless you mess up your Python installation and use sys.setdefaultencoding() to change that default. You cannot do that normally, because the sys.setdefaultencoding()
function is deleted from the module at start-up time, but there is a Cargo Cult going around where people use reload(sys)
to reinstate that function and change the default encoding to something else to try and fix implicit encoding and decoding problems. This is a dumb thing to do for precisely this reason.
来源:https://stackoverflow.com/questions/28627444/will-a-unicode-string-just-containing-ascii-characters-always-be-equal-to-the-as