<Python> 空の判定。
空 から
vacancy
null
のものの判定。
次の関数を用意。
In [1]: def j(x): ...: if x: ...: print('"if" catches it') ...: if not x: ...: print('"if not" catches it') ...:
で、いくつかのタイプ type
で判定。
bool
In [2]: x = True In [3]: type(x) Out[3]: bool In [4]: j(x) "if" catches it In [5]: x = False In [6]: j(x) "if not" catches it
str
In [7]: x = '' In [8]: type(x) Out[8]: str In [9]: j(x) "if not" catches it In [11]: x = 'hage' In [12]: j(x) "if" catches it
NoneType
In [15]: x = None In [16]: type(x) Out[16]: NoneType In [17]: j(x) "if not" catches it
なるほど。
空かどうか?は、if
だけで簡単に判定できるっぽい。
いいねー。