Python if not == vs if !=

后端 未结 7 1375
轻奢々
轻奢々 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:33

    I want to expand on my readability comment above.

    Again, I completely agree with readability overriding other (performance-insignificant) concerns.

    What I would like to point out is the brain interprets "positive" faster than it does "negative". E.g., "stop" vs. "do not go" (a rather lousy example due to the difference in number of words).

    So given a choice:

    if a == b
        (do this)
    else
        (do that)
    

    is preferable to the functionally-equivalent:

    if a != b
        (do that)
    else
        (do this)
    

    Less readability/understandability leads to more bugs. Perhaps not in initial coding, but the (not as smart as you!) maintenance changes...

提交回复
热议问题