pythonで文字列が0のとき、Noneのとき、dict形式のkeyが無いときに引っ掛けるif文を作ったのですが、もう少し短く書く方法は無いでしょうか
# 以下のパターンを引っ掛けたい dic = {"hello": "hello"} dic = {"test": ""} dic = {"test": " "} dic = {"test": None} # このif文をきれいに書きたい # dic['test']がNoneの場合エラーになる if 'test' in dic and len(dic['test']) == 0 and dic['test'] is not None : test = dic['test'] # 追記 if 'test' in dic and dic['test'] is not None and len(dic['test']) == 0 : test = dic['test'] else: print("error")
よろしくお願いします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/13 08:14