1. if not x
直接使用 x 和 not x 判断 x 是否为 None 或空
x = [1,3,5]
if x:
print('x is not empty ')
if not x:
print('x is empty')
下面写法不够 Pythoner
if x and len(x) > 0:
print('x is not empty ')
if x is None or len(x) == 0:
print('x is empty')
直接使用 x 和 not x 判断 x 是否为 None 或空
x = [1,3,5]
if x:
print('x is not empty ')
if not x:
print('x is empty')
下面写法不够 Pythoner
if x and len(x) > 0:
print('x is not empty ')
if x is None or len(x) == 0:
print('x is empty')