Python if not

Source: Internet
Author: User

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
  1. >>> x = 1
  2. >>> not x
  3. False
  4. >>> x = [1]
  5. >>> not x
  6. False
  7. >>> x = 0
  8. >>> not x
  9. True
  10. >>> x = [0] # you don ' t want to fall in this one.
  11. >>> not x
  12. False


In Python none, False, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () are equivalent to false, i.e.:

[Python]View Plaincopy
    1. <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
  1. >>> x = []
  2. >>> y = None
  3. >>>
  4. >>> x is None
  5. False
  6. >>> y is None
  7. True
  8. >>>
  9. >>>
  10. >>> not x
  11. True
  12. >>> not y
  13. True
  14. >>>
  15. >>>
  16. >>> not x is None
  17. >>> True
  18. >>> not y is None
  19. False
  20. >>>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.