Tag:details copy logic judgment ant ping print sel net usage
Python if not determines whether the case is none if the Xif x is Noneif isn't x is none if the X is not none "is the best way to do it, clear, without errors, and persist in using this notation 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 {}, empty tuple () does not affect your judgment at all. ============== Reprint to Http://blog.csdn.net /sasoritattoo/article/details/12451359========== code often has a variable is the judge of None, there are three main ways: the first is ' if X is None '; the second is ' if not x: '; the third Yes ' 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 F Alse >>> 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 () equals false, i.e.: [Python] View plaincopy& Lt;strong>not None = = not False = = not ' = = not 0 = = not [] = = not {} = = not () </strong> so when using the list, if you want to differentiate x==[] And X==none two cases, this time ' if not x: ' will cause problems: [Python] view plaincopy>>> x = [] >>> y = none >>> >>> x is none False >>> y is none True >>> >>&G T >>> not x True >>> not y true >>> >>> >>> isn't x is None >>> True >>> not Y is none False >>> Perhaps you are 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 clear that the former is clearer and the latter may misinterpret the reader as ' if ' is none ', so recommending the former, and this is also the Google recommended style conclusion: ' If X is not None ' is the best writing, clear, no mistakes, and later insist on using this writing. 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. ================================================================ but this does not apply to the case where the variable is a function, the following is reproduced from: https://github.com/ Wklken/stackoverflow-py-top-qa/blob/master/contents/qa-control-flow.md Foo is none and foo = = None difference problem link 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 In addition Python does not specifically indicate what is, For example, the heartfelt thanks to Boolean true and False,not true for False,not false to True, here are a few common uses of not: (1) Not with logical judgment if the expression is false, Executes the statement following the colon. 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 an element, 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
Brother even learn python---if not