我有一段代码,如下所示:
a = None
b = None
c = None
d = None
if a==None and b==None and c==None and d==None:
print("All are None")
else:
print("Someone is not None")
Run Code Online (Sandbox Code Playgroud)
当我有更多数量的变量需要像这样检查时,实现此目的的Pythonic方法或更短的方法是什么?
您可以使用列表,并使用列表理解检查列表中的所有项目:
l_list = [None, None, None, None, None]
if all(item is None for item in l_list):
print("All items are None")
Run Code Online (Sandbox Code Playgroud)