pythonにおけるFalseの判定とIn
2079 ワード
import json
def checkFalse(obj):
if obj:
print(f"{json.dumps(obj,ensure_ascii=False)}:True")
else:
print(f"{json.dumps(obj,ensure_ascii=False)}:False")
def checkExcuteFalse(script, obj):
if obj:
print(f"{script}:True")
else:
print(f"{script}:False")
checkFalse(None)
checkFalse({})
checkFalse([])
checkExcuteFalse("()",())
checkExcuteFalse("set()",set())
checkFalse("")
checkFalse(" ")
checkFalse('')
checkFalse(' ')
checkFalse(True)
checkFalse(False)
checkFalse(0)
checkFalse(0.0)
checkFalse(1)
checkFalse(-1)
checkExcuteFalse("1 in [1, 2]", 1 in [1, 2])
checkExcuteFalse("1 in [2]", 1 in [2])
checkExcuteFalse("1 in []", 1 in [])
checkExcuteFalse("1 in {'1': 5}", 1 in {"1": 5})
checkExcuteFalse("'1' in {'1': 5}", '1' in {"1": 5})
checkExcuteFalse("'a' in 'abc'", 'a' in 'abc')
checkExcuteFalse("1 in (1,'an')", 1 in (1,'an'))
checkExcuteFalse("[] in (1,[])", [] in (1,[]))
checkExcuteFalse("[1,2] in [1,2,3]", [1,2] in [1,2,3])
checkExcuteFalse("None in [None,2,3]", None in [None,2,3])
checkExcuteFalse("1 in set([1,2])", 1 in set([1,2]))
結果
null:False
{}:False
[]:False
():False
set():False
"":False
" ":True
"":False
" ":True
true:True
false:False
0:False
0.0:False
1:True
-1:True
1 in [1, 2]:True
1 in [2]:False
1 in []:False
1 in {'1': 5}:False
'1' in {'1': 5}:True
'a' in 'abc':True
1 in (1,'an'):True
[] in (1,[]):True
[1,2] in [1,2,3]:False
None in [None,2,3]:True
1 in set([1,2]):True
結論
一般的に無効なデータ、例えば、空文字列、空の集合、空のリスト、空のメタグループ、None、空の辞書、数字0は、Fasle Inオペレータの位置決めに使用できると判断されます.辞書のKeyを判定するだけでなく、