Python的None代表空,不含任何值,相當於其他語言(e.g. Java, C#, php)的null。Python判斷變數是否為None的方式如下。
使用is None判斷是為None;
使用is not None判斷不為None。
a = None
if a is not None:
print("a is not None")
else:
print("a is None")
b = 1
if b is not None:
print("b is not None")
else:
print("b is None")
print(a is None) # True
print(b is not None) # True
印出結果
a is None
b is not None
True
True
沒有留言:
張貼留言