比较字节和字符串对象时,Python 3 返回“False”。
>>> b'' == ''
False
Run Code Online (Sandbox Code Playgroud)
有没有办法强制解释器引发异常?也许调用 python3 时有一些标志?
将-b标志传递给 Python3 将在比较字节和字符串时发出警告。通过 -bb 将引发异常。
$ python3 -c "b'' == '' "
$ python3 -bc "b'' == '' "
<string>:1: BytesWarning: Comparison between bytes and string
$ python3 -bbc "b'' == '' "
Traceback (most recent call last):
File "<string>", line 1, in <module>
BytesWarning: Comparison between bytes and string
Run Code Online (Sandbox Code Playgroud)