我在想是否要检查字符串或字符的布尔值,是否要检查它们是否为空。但是以下代码给了我意外的输出。
print('0'==True)
print("0"==True)
Run Code Online (Sandbox Code Playgroud)
输出:
False
False
Run Code Online (Sandbox Code Playgroud)
怎么了?我们真正在检查什么?
它们是正确的(在布尔上下文中):
if '0':
print("you will see this")
if '': # for comparison
print("you will not see this")
# Alternately:
bool('0') # True
bool('') # False
Run Code Online (Sandbox Code Playgroud)
但是它们不等于特殊值True。
没有矛盾。
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |