Python: `if not x:` VS `if x is not None:` VS `if not x is None:`

There are three main ways to judge whether a variable is none in code

The first is’ if x is none ‘

The second is’ if not X: ‘

The third is “if not x is none”

If you think it doesn’t make any difference, you should be careful. There is a hole in it. Let’s look at the code first

>>>x=1
>>>notx
False
>>>x=[1]
>>>notx
False
>>>x=0
>>>notx
True
>>>x=[0]#Youdon'twanttofallinthisone.
>>>notx
False

In Python, none, false, empty string “” 0, empty list [, empty dictionary {}, empty tuple () are equivalent to false, that is:

<strong>notNone==notFalse==not''==not0==not[]==not{}==not()</strong>

Therefore, when using the list, if you want to distinguish between X = = [] and x = = none, there will be a problem with ‘if not X:’:

>>>x=[]
>>>y=None
>>>
>>>xisNone
False
>>>yisNone
True
>>>
>>>
>>>notx
True
>>>noty
True
>>>
>>>
>>>notxisNone
>>>True
>>>notyisNone
False
>>>

Maybe you want to judge whether x is none or not, but you also judge the case of ‘x = =]’, in which case you will not be able to distinguish

for those who are used to using the writing method of if not x, it must be clear that when x equals none, false, empty string “” 0, empty list [], empty dictionary {}, empty tuple (), it will not affect your judgment

As for the writing of ‘if x is not none’ and ‘if not x is none’, it is obvious that the former is clearer, while the latter may make readers misunderstand it as’ if (not x) is none ‘, so the former is recommended, which is also the style of Google recommendation
at the same time

conclusion:

‘if x is not none’ is the best way to write. It is clear and will not make mistakes. I will insist on using this way in the future

the premise of using if not x is: it must be clear that when x equals none, false, empty string “” 0, empty list [], empty dictionary {}, empty tuple (), it will not affect your judgment

================================================================

However, this does not apply to the case where the variable is a function, which is reproduced from: https://github.com/wklken/stackoverflow-py-top-qa/blob/master/contents/qa-control-flow.md

The difference between foo is none and foo = = none

Question link

if foo is None: pass
if foo == None: pass

If the same object instance is compared, is always returns true and = = depends on “ EQ ()”

>>> class foo(object):
    def __eq__(self, other):
        return True

>>> f = foo()
>>> f == None
True
>>> f is None
False

>>> list1 = [1, 2, 3]
>>> list2 = [1, 2, 3]
>>> list1==list2
True
>>> list1 is list2
False

In addition

(ob1 is ob2) IS (id(ob1) == id(ob2))

################################################################################

Supplement, 2013.10.09

What does not mean in Python?For example, thank you very much

In python not is a logical judgment word for boolean True and False, not True for False and not False for True. Here are a few common uses of not.
(1) not is used in conjunction with the logical judgment sentence if, representing the statement after the colon is executed when the expression after not is False. For example
a = False
if not a: (Here, because a is False, not a is True)
    print "hello"
Here we can output the result hello
(2) Determine whether the element is in the list or dictionary, if a not in b, a is the element, b is the list or dictionary, this sentence means that if a is not in the list b, then execute the statement after the colon, for example
a = 5
b = [1, 2, 3]
if a is not in b:
    print "hello"
The result hello can also be output here
not x is equivalent to     if x is false, then True, else False

Similar Posts: