The Python code ' if not x: ' and ' If X is not none: ' and ' if not X is none: ' Using

Source: Internet
Author: User

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
  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

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

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.