Python code if not x: And if x are not none: And if not X are none: Use introduction _python

Source: Internet
Author: User
Tags in python

There are three main ways in which a variable is always judged to be none:
The first is "if X is None";
The second type is ' if not x: ';
The third is ' if not ' (this is a clearer understanding of ' if not ' (x is None) ').
If you think it makes no difference, then you should be careful, there is a hole in it. Let's take a look at the code:

>>> x = 1
>>> not x
false
>>> x = [1]
>>> not x
False
>& gt;> x = 0
>>> not x
True
>>> x = [0]     # you don ' t want to fall
in this one. >>> not x
False

None in Python, false, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () is equal to false, namely:

Copy Code code as follows:

Not None = = not False = = not ' = = not 0 = not [] = = not {} = Not ()

So when using a list, if you want to differentiate between x==[] and x==none two situations, then ' if not x: ' There will be a problem:

>>> x = []
>>> y = none
>>> 
>>> x is None
False
>>> y None
True
>>> 
>>> 
>>> not x
true
>>> not y
True
>>> 
>>> 
>>> not X are None
>>> True
>>> not y is None
False

Perhaps you are trying to determine if X is None, but you are also judging the situation of ' x==[', in which 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.
For the ' if ' not none ' and ' if not ' ' none ' notation, it is obvious that the former is clearer, and the latter may misinterpret the reader as ' if (not X ') ' None ', and therefore recommend the former, which is also the recommended style of Google

Conclusion:
' If ' isn't None ' is the best way to spell, clear, no mistakes, and later insist on using this notation.
The premise of using if not X is that it must be clear that x equals None, False, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () has no effect on 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 links

If Foo is None:pass
if foo = = None:pass

If you compare the same object instance, the is always returns true and = = ultimately depends on the EQ ()

>>> class Foo (object):
  def __eq__ (self, Other): Return
    True

>>> f = foo ()
>> > f = = None
True
>>> f
is none False

>>> list1 = [1, 2, 3]
>>> list2 = [1, 2, 3]
>>> list1==list2
True
>>> list1 is List2
False

Other than that

(Ob1 is OB2) equivalent to (ID (ob1) = = ID (ob2))

################################################################################
Added, 2013.10.09
Transferred from http://zhidao.baidu.com/question/514056244.html

What is not specifically in Python, for example, is heartfelt thanks

In Python, not is a logical word, used for Boolean true and False,not true to false,not false to True, and the following are several common uses of not:
(1) Not and the logical judgment sentence if used to execute the statement after the colon, when the expression after not is false. Like what:
A = False
If not a: (here because A is false, not A is true)
print "Hello"
Here we can output the results hello
(2) to determine whether an element is in a list or a dictionary, if a isn't in B,a is an element, B is a list or a dictionary, meaning that if a is not in list B, then execute the statement following the colon, such as:
A = 5
b = [1, 2, 3]
If a isn't in B:
print "Hello"
We can also output the results here Hello

Not x means equivalent to if X is false, then True, else false

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.