The case of whether or not to judge none
If not X
If X is None
If not X is None
If X is not None "is the best way to do it, clear, without errors, and persist in using this notation later.
Use if not x this is the premise that you must be clear that x equals None, False, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () does not affect your judgment.
============== Reprint to http://blog.csdn.net/sasoritattoo/article/details/12451359==========
There are three main ways to judge whether a variable is a none in the code:
The first is the ' if X is None ';
The second type is ' if not x: ';
The third kind of ' if not X is None ' (this sentence understands more clearly ' if not ' (x is None) ').
If you think it makes no difference, then you should be careful, there is a hole in it. First look at the code:
[Python]View Plaincopy
- >>> x = 1
- >>> not x
- False
- >>> x = [1]
- >>> not x
- False
- >>> x = 0
- >>> not x
- True
- >>> x = [0] # you don ' t want to fall in this one.
- >>> not x
- False
In Python none, False, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () are equivalent to false, i.e.:
[Python]View Plaincopy
- <strong>not None = = Not False = = not "= = not 0 = = Not [] = = Not {} = = not () </strong>
So when using the list, if you want to differentiate between x==[] and x==none two cases, then ' if not x: ' Will cause problems:
[Python]View Plaincopy
- >>> x = []
- >>> y = None
- >>>
- >>> x is None
- False
- >>> y is None
- True
- >>>
- >>>
- >>> not x
- True
- >>> not y
- True
- >>>
- >>>
- >>> not x is None
- >>> True
- >>> not y is None
- False
- >>>
You may be trying to determine if X is None, but you have judged the situation of ' x==[', and in this case it will be indistinguishable.
For Pythoner accustomed to using if not X, it must be clear that x equals None, False, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () does not affect your judgment.
And for ' If X is not none ' and ' if not X is None ', it is obvious that the former is clearer, and the latter may misinterpret the reader as ' if ' is none ', so recommend the former, and this is Google's recommended style
Conclusion:
' If x is isn't None ' is the best way to do it, clear, without errors, and stick with it later.
The premise of using if not X is that you must be clear that x equals None, False, empty string "", 0, empty list [], empty Dictionary {}, and empty tuple () do not affect your judgment.
================================================================
However, this does not apply to the case where the variable is a function, reproduced below 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
Problem link
如果比较相同的对象实例,is总是返回True 而 == 最终取决于 "eq()"
>>> class foo(object): def __eq__(self, other): return True>>> f = foo()>>> f == NoneTrue>>> f is NoneFalse>>> list1 = [1, 2, 3]>>> list2 = [1, 2, 3]>>> list1==list2True>>> list1 is list2False
Other than that
the not in Python is exactly what it is, for example, a heartfelt thanks
Boolean true and False,not true for False,not false to True, the following are a few common uses of not: (1) do not with the logical judgment if the expression is false, and the statement after the colon is executed. For example: a = Falseif not a: (here because A is false, so not A is true) print "Hello" here can output the result Hello (2) determine whether the element is in a list or dictionary, if a not in B,a is a meta b is a list or dictionary, which means that if a isn't in list B, then the statement after the colon is executed, such as: a = 5b = [1, 2, 3]if a not in B: print "Hello" can also output the result hello
Python if not