竖条警告-无法下标的对象

问题描述 投票:1回答:1

以下代码在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)
python visual-studio-code pylint
1个回答
0
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.