检查字符串int>;如何实现?你知道吗
>>> strver = "1"
>>> ver = 1
>>> strver > ver
True
>>> strVer2 = "whaat"
>>> strVer2 > ver
True
做了更多的实验:
>>> ver3 = 0
>>> strVer2 > ver3
True
我认为在尝试比较时应该有一个错误,但似乎没有生成任何东西来处理这样的错误,或者应该使用assert
,但是如果python代码是用-O标志运行的,那么这可能是危险的!你知道吗
Tags:
来源:How does Python compare string and int?,它反过来引用了CPythonmanual:
从答案来看:
……所以,这是因为字母表中“s”在“i”之后!不过,幸运的是,在Python 3.x的实现中,这个稍微奇怪的行为已经被“修复”:
现在似乎遵循最小惊奇的原则好一点了。你知道吗
相关问题 更多 >
编程相关推荐