如何强制 Python 3 在比较字节和字符串时引发异常

mec*_*ner 4 python python-3.x

比较字节和字符串对象时,Python 3 返回“False”。

>>> b'' == ''
False
Run Code Online (Sandbox Code Playgroud)

有没有办法强制解释器引发异常?也许调用 python3 时有一些标志?

sna*_*erb 6

-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)