Python で None を判断する 3 つの方法

        1. x がなしの場合
        2. x でない
        場合 3. x がなしの場合

        Python では、None、False、空の string''、空の list[]、および空の tuple() は、実際には False と同等ですxが空リスト、yがNoneの場合、xがNoneであると判定するとFalseとなり、xではないと判定するとTrue、つまり空リストは実際にはFalseになります。したがって、1 番目と 2 番目のメソッドでは、x==[] と x==Noneの場合を区別できません。

        次に、3 番目の方法を使用して、x が空のリストか None かを区別します。同様に、x が空のリストの場合、not x is None の結果は True です。x が None の場合、not x is None の結果はFalse なので、3 番目の方法を使用すると、x が空のリスト [] であるか None であるかを区別できます。

# Three method of judging variable is None or Not

# 1.if x is None
# 2.if not x
# 3.if not x is None

x = []
y = None
print("not x is:", not x)
print("not y is:", not y)
print("not x is None is:", not x is None)
print("not y is None is:", not y is None)

おすすめ

転載: blog.csdn.net/qq_36171491/article/details/124728283