看一下下面的代码的输出:
def match(word)
li = [
'good',
'job'
'dq'
]
return word in li
if __name__ == '__main__':
print(match('dq'))
输出: False
def match(word)
li = [
'good',
'job',
'dq'
]
return word in li
if __name__ == '__main__':
print(match('dq'))
输出: True
line:5 python 列表中这种写法并不会报错,后面两个字符串被认为是一个字串。
这个问题很难通过肉眼排查发现,目前没有更好的方案回避这个问题。
本文探讨了一个Python代码中的奇特现象,即列表中看似错误的写法实际上并未报错,且在`match`函数中返回了不同预期的结果。作者揭示了这一行为背后的逻辑,并指出排查此类问题的挑战。
822

被折叠的 条评论
为什么被折叠?



