None in Python

Source: Internet
Author: User

None is an object whose type is Nonetype, and its bool value is false, like 0 is an object whose type is int and its bool value is false, while in Python the bool value is false in the following ways:

Ling Jian

Links: https://www.zhihu.com/question/48707732/answer/112233903
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

This is actually written in the Python document, and for the sake of accuracy, we'll refer to the original text in the Python document:
In the
context of a Boolean operations, and also when expressions is used Bycontrol flow statements, the following VA Lues is interpreted as False:false, None, numeric zero of all types, and empty strings and containers (including strings, Tuples, lists, dictionaries, sets and frozensets). Allother values is interpreted as true. (See the __nonzero__ () special method for A.)

When making logical judgments (such as if), the value in Python that equals false is not only false, it also has a set of rules. For basic types, there is basically one value for each type that is judged to be false. This is roughly the case:
    1. Boolean, False for false, other true
    2. Integers and floating-point numbers, 0 for false, others true
    3. string and class string types (including bytes and Unicode), empty strings represent false, others are true
    4. Sequence type (including Tuple,list,dict,set, etc.), NULL for false, non-null to indicate true
    5. None always means false

Objects of the custom type are subject to the following rules:

    1. If the __nonzero__ () method is defined, the method is called and the return value is used to determine whether the object is equivalent to TRUE or false.
    2. If the __nonzero__ method is not defined but the __len__ method is defined, the __len__ method is called, and true if return 0 o'clock is false (this corresponds to False if the built-in type is empty)
    3. If none is defined, all objects are true, only none corresponds to False
1>>>classA:2     def __nonzero__(self):3         return04 5     6>>> B =A ()7>>>ifB:8     Print "haha"9 Ten      One>>>if  notB: A     Print "haha" -  -      the haha ->>>ifA: -     Print "haha" -  +      -haha


So exactly, this sentence of yours (state is the dictionary, get method is to determine whether the key in the dictionary has the first argument, not the second parameter returned)

state = states.get(‘texas‘, None)if not state: ....

Equivalent to

if ‘texas‘ not in states or states[‘texas‘] is None or not states[‘texas‘]: ...

It has three types of cases:

    1. does not exist in Dict
    2. Dict exists, but the value is None
    3. Dict exists and is not none, but is a value equivalent to false, such as an empty string or an empty list.

None in Python

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.