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:
- Boolean, False for false, other true
- Integers and floating-point numbers, 0 for false, others true
- string and class string types (including bytes and Unicode), empty strings represent false, others are true
- Sequence type (including Tuple,list,dict,set, etc.), NULL for false, non-null to indicate true
- None always means false
Objects of the custom type are subject to the following rules:
- 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.
- 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)
- 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:
- does not exist in Dict
- Dict exists, but the value is None
- 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