![](/img/trans.png)
[英]dict.get(None) on a dict with None value takes default arg
[英]Dict: get() not returning 0 if dict value contains None
python dict是否具有值None的鍵,並在調用get()時返回NoneType
ex_dict = {"test" : None} ex_dict.get('test', 0)
在上面的示例中,它應該返回0,但不會返回。
誰能解釋為什么會這樣。
不,不應該。 如果在ex_dict
字典中找不到test
鍵,則應返回0
。 但是,因為它存在,它將返回None
ex_dict = {"test" : None}
print type(ex_dict.get('test', 0)) # <class 'NoneType'>, 'test' exist, return None
print(ex_dict.get('hello', 0)) # prints 0, 'hello' isn't a key inside ex_dict
ex_dict.get('test', 0)
的None
響應是ex_dict.get('test', 0)
因為“ test”鍵存在並且具有None
值。 例如,如果您使用ex_dict.get("non_existing_key", 0)
嘗試相同操作,則返回0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.