Python code if not x: And if x is not None: And if not x is None: usage introduction, pythonnone

Source: Internet
Author: User

Python code if not x: And if x is not None: And if not x is None: usage introduction, pythonnone

There are usually three main methods to determine whether the variable is None in the Code:
The first is 'if x is none ';
The second type is 'If not x :';
The third is 'If not x is none' (this sentence is more clear, 'If not (x is None )').
If you think there is no difference in writing this, you should be careful. There is a pitfall in it. Let's take a look at the Code:

>>> x = 1>>> not xFalse>>> x = [1]>>> not xFalse>>> x = 0>>> not xTrue>>> x = [0]     # You don't want to fall in this one.>>> not xFalse

In python, None, False, empty string "", 0, empty list [], empty dictionary {}, empty tuples () are equivalent to False, that is:

Copy codeThe Code is as follows:
Not None = not False = not ''= not 0 = not [] = not {} = not ()

Therefore, when using the list, if you want to distinguish between x = [] and x = None, 'If not x: 'will cause a problem:

>>> x = []>>> y = None>>> >>> x is NoneFalse>>> y is NoneTrue>>> >>> >>> not xTrue>>> not yTrue>>> >>> >>> not x is None>>> True>>> not y is NoneFalse>>> 

Maybe you want to determine whether x is None, but you can also judge the case of 'X = [] '. In this case, it cannot be distinguished.
For pythoner, which is used to writing if not x, it must be clear that x is equal to None, False, empty string "", 0, empty list [], empty dictionary {}, empty tuples () does not affect your judgment.
For 'if x is not none' and 'If not x is none', it is obvious that the former is clearer, while the latter may cause readers to misunderstand it as 'If (not x) is none'. Therefore, the former is recommended, which is also recommended by Google.

Conclusion:
'If x is not none' is the best method, which is clear and will not cause errors. We will stick to this method later.
The premise for using if not x is that x is equal to None, False, empty string "", 0, empty list [], empty dictionary {}, empty tuples () does 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

The difference between foo is None and foo = None

Problem Link

If foo is None: pass
If foo = None: pass

If the instance of the same object is compared, is always returns True and = is ultimately dependent 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

(Ob1 is ob2) is equivalent to (id (ob1) = id (ob2 ))

######################################## ########################################
Supplement: 2013.10.09

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.