以下代码在Visual Studio代码编辑器中被截断
a = b.get('c') if b else None
d = a[1] if a else None
pylint
在第二行中为a[1]
发出以下警告。显示警告是否正确? None
的支票不应该涵盖吗?
a: NoneType
Value 'a' is unsubscriptable pylint(unsubscriptable-object)
pylint未能检测到正确的类型,您可以通过以下方式禁止显示警告:
d = a[1] if a else None # pylint disable=unsubscriptable-object
或(因为var b不在您的帖子中),这是正确的,并且b.get('c')
返回无法下标的类型,例如:
b = {"c": 1}
a = b.get('c') if b else None
# a = 1, 1 is not subscriptable