Pycharm 与 None 的比较使用相等运算符
Pycharm comparison with None performed with equality operators
我正在使用 python 3.6 和 pycharm。
我的一行代码说:
If oim.sent != None:
Pycharm 给出以下措辞非常强烈的警告:
That type of comparisons should always be done with 'is' or 'is not',
never the equality operators.
但是,我正在使用这行代码来区分 None
的值和 False
的值 - 我这样写这行是对的还是真的你永远不应该在 None?
中使用相等运算符
编辑:
我误解了警告。我认为它是在指示我使用 if not oim.sent:
,这当然不会区分 None
和 False
- 但是,正如答案所指出的那样,正确的表达方式是 if oim.sent is not None:
这只是一个警告,因为您没有遵循 python 指南。您的代码 运行 没问题。如果你愿意,你最好使用 :
if oim.sent is not None:
你当然可以使用!= None
,但是
if oim.sent is not None:
速度更快,而且符合惯用语 Python 所以每个人都会知道您知道自己在做什么 ;-)
我正在使用 python 3.6 和 pycharm。
我的一行代码说:
If oim.sent != None:
Pycharm 给出以下措辞非常强烈的警告:
That type of comparisons should always be done with 'is' or 'is not', never the equality operators.
但是,我正在使用这行代码来区分 None
的值和 False
的值 - 我这样写这行是对的还是真的你永远不应该在 None?
编辑:
我误解了警告。我认为它是在指示我使用 if not oim.sent:
,这当然不会区分 None
和 False
- 但是,正如答案所指出的那样,正确的表达方式是 if oim.sent is not None:
这只是一个警告,因为您没有遵循 python 指南。您的代码 运行 没问题。如果你愿意,你最好使用 :
if oim.sent is not None:
你当然可以使用!= None
,但是
if oim.sent is not None:
速度更快,而且符合惯用语 Python 所以每个人都会知道您知道自己在做什么 ;-)