【发布时间】:2016-06-20 14:49:51
【问题描述】:
即使我检查字典中是否存在密钥,我也会收到一个密钥错误:
def foo(d):
if (('element' in d.keys()) & (d['element'] == 1)):
print "OK"
foo({})
在documentation我们可以阅读:
表达式 x 和 y 首先计算 x;如果 x 为假,则其值为 回来;否则,对 y 求值,结果值为 返回。
谁能解释一下这种行为?
【问题讨论】:
-
尝试将 '&' 替换为 'and'
-
您正在查看
and的文档并执行&,其文档为here(在同一页面上)。 -
你为什么用空字典调用 foo()?
-
其实我已经投过简历了,但这可能是重复的:stackoverflow.com/questions/22646463/…
标签: python if-statement dictionary keyerror