Why does comparison of bytes with str fails in Python3

时光总嘲笑我的痴心妄想 提交于 2019-11-30 08:28:49

In Python 2.x, the design goal for unicode is to enable transparent operations between unicode & byte strings by implicitly converting between the 2 types. When you do the comparison u"" == "", the unicode LHS is automatically encoded into a byte string first, and then compared to the str RHS. That's why it returned True.

In contrast, Python 3.x, having learned from the mess of unicode that was in Python 2, decided to make everything about unicode vs. byte strings explicit. Thus, b"" == "" is False because the byte string is no longer automatically converted to unicode for comparison.

In python 3 string is Unicode . The type used to hold text is str and the type used to hold data is bytes.

the str and bytes types cannot be mixed, you must always explicitly convert between them. Use str.encode() to go from str to bytes, and bytes.decode() to go from bytes to str.

Therefore, if you do b"".decode() == "" you'll get True :

>>> b"".decode() == ""
True

For more info read Text Vs. Data Instead Of Unicode Vs. 8-bi

The designers decided to not assume an encoding for coercion when comparing bytes to strings, so it falls under the default behavior of Python 3.x whereby comparisons containing differing types fail.

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