Python 中的 None 与真假
2016-08-27 16:08
429 查看
Python 中 0 为假,大小为 0 的容器也定义为假;
空字符串与空的列表也为假:
None 可作为一个对象,该对象的类型为:NoneTye
None 表示的含义,更多的是一种不存在,是真正的空,而不是空列表(
比如一个函数的没有返回值
id(None):506046248L
None == None
True, 比较的是值的大小
None is None
True,是否为同一个对象
not None:True
任何数都与 None 可比,且任何数都大于 None
但不可以使用 None(None 是一个对象,是一个 NoneType 对象):
空字符串与空的列表也为假:
None 可作为一个对象,该对象的类型为:NoneTye
None 表示的含义,更多的是一种不存在,是真正的空,而不是空列表(
[])的空,
比如一个函数的没有返回值
def f(): pass >> f() >> a = f() >> a is None True
id(None):506046248L
None == None
True, 比较的是值的大小
None is None
True,是否为同一个对象
not None:True
任何数都与 None 可比,且任何数都大于 None
>> 1 > None True >> -1 > None True >> -43758297596 > None True >> 5 > None False
1. 基本数据类型的判空
比如 list 的判空,参见 一题多解(一) —— list(Python)判空(以及 is 与 == 的区别);但不可以使用 None(None 是一个对象,是一个 NoneType 对象):
>> [] == None False >> [] is None False >> a = [] >> a == [] True
相关文章推荐
- python none
- Python list中的[None] & [0]
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- Python错误:AssertionError: group argument must be None for now
- python PIL bug: 'NoneType' object has no attribute 'bands' (解决)
- Python出现TypeError: 'NoneType' object is not iterable
- python 中的特殊常量 None
- 关于python中的none
- python 返回值(return)None
- 关于Python的列表append()返回None对象
- [Python][转]Exception AttributeError: "'NoneType' object has no attribute
- python is not None
- 关于PYTHON中的none+range解释
- python 3中对list进行sort时,返回值为None
- 第二章Python下的set,dictionary,none
- 4.python字典和None类型
- python查询数据库,打印查询结果过程中出现'NoneType' object is unsubscriptable
- 《python核心编程》序列:字符串、列表和元组Python: Attribute Error - 'NoneType' object has no attribute
- python中的None
- Python标准库:内置函数eval(expression, globals=None, locals=None)