Python if not == vs if !=

后端 未结 7 1350
轻奢々
轻奢々 2020-12-07 08:09

What is the difference between these two lines of code:

if not x == \'val\':

and

if x != \'val\':

Is one

相关标签:
7条回答
  • 2020-12-07 08:41

    An additional note, since the other answers answered your question mostly correctly, is that if a class only defines __eq__() and not __ne__(), then your COMPARE_OP (!=) will run __eq__() and negate it. At that time, your third option is likely to be a tiny bit more efficient, but should only be considered if you NEED the speed, since it's difficult to understand quickly.

    0 讨论(0)
提交回复
热议问题