Content transfer from
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
if foo is None: passif foo == None: pass
If you compare the same object instance, is always returns true and = = ultimately depends on "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
(ob1 is ob2) 等价于 (id(ob1) == id(ob2))
Transfer from http://zhidao.baidu.com/question/514056244.html
the not in Python is exactly what it is, for example, a heartfelt thanks
In Python, not is a logical judgment word, for Boolean true and False,not true for False,not false to True, here are a few common usages of not: (1) Not with logical judgment if the The statement after the colon is executed when the expression that represents the not is false. 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
Not x means equivalent if X is false and then True, else false
The Python code ' if not x: ' and ' If X is not none: ' and ' if not X is none: ' Using