Will a UNICODE string just containing ASCII characters always be equal to the ASCII string?

让人想犯罪 __ 提交于 2019-12-21 06:59:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!