問題:
My editor warns me when I compare my_var == None
, but no warning when I use my_var is None
.當我比較my_var == None
,我的編輯器會警告我,但當我使用my_var is None
時沒有警告。
I did a test in the Python shell and determined both are valid syntax, but my editor seems to be saying that my_var is None
is preferred.我在 Python shell 中做了一個測試並確定兩者都是有效的語法,但我的編輯器似乎在說my_var is None
是首選。
Is this the case, and if so, why?是這樣嗎,如果是,爲什麼?
解決方案:
參考一: https://stackoom.com/question/xmOL參考二: Python None comparison: should I use “is” or ==?