Python 来开个会会:Python 里面的 None

余嬢嬢 · 2021年03月13日 · 最后由 L. 回复于 2021年06月15日 · 2308 次阅读

首先声明,写在这里只是我个人的一个记录方式,可能有不完美的地方,只是为了自己加深记忆。

在 Python 中,有一个特殊的常量 None(N 必须大写)。和 False 不同,它不表示 0,也不表示空字符串,而表示没有值,也就是空值。它是一个特殊 Python 对象, None 的类型是 NoneType

>>>type(None)
<class 'NoneType'> #输出<class 'NoneType'>,可以看到它是一个对象

None 在 Python 解释器启动时自动创建对象, 解释器退出时销毁。

None 是 NoneType 数据类型的唯一值(其他编程语言可能称这个值为 null、nil 或 undefined),也就是说,我们不能再创建其它 NoneType 类型的变量,但是可以将 None 赋值给任何变量。

那么 None 到底是啥呢,是表示空吗?
代码试试

>>a=[]
>>>a == None
False  #看到了吗,返回的是False

在试试另外的

>>>a is None
False  #依然False,说明None和空列表不一样

那么我们在试试 0 呢

>>>0==None
False#依然False,说明None和0也不一样

那么我们在试试空字符串

>>>''==None
False#依然False,说明None和空字符串也不一样

ok,那我们再试试一个未定义过的变量呢

>>>asd==None
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'asd' is not defined
#报错了,哈哈哈,这个方法行不通

最后试试 bool 类型呢

>>>False == None
False #依然不是哈

但是注意一点,在 if 判断中,None 代表 False

if None:
    print('True')
else:
    print('False')
#输出False

那么这个 None 到底是啥,能干啥?
简单来说,None 就是没有,不存在,0 和空字符串,空序列虽然为空,但是他们是存在的,是有意义的

None 有啥用处呢
写个代码说明一下

def nonetest():
    print('this is nonetest')

a=nonetest()
print(a)

运行后:
this is nonetest
None
这个时候 a 的值就是 None 了,因为函数 nonetest 没有返回值,a 就是没有,就是 None

在修改一下文件的代码:

def nonetest():
    print('this is nonetest')
    return
a=nonetest()
print(a)

运行后:
this is nonetest
None
这个时候 a 的值依然是 None,因为函数 nonetest 的返回值是空

所以总结一下,None 就是没有,啥也不是,不是 0,也不是空字符串,也不是 False(在 if 条件语句中相当于 False),也不是空序列

共收到 6 条回复 时间 点赞

这篇文章挺有意思的,特别在没有返回值的函数的应用中注意这个 ‘None’,比如,我们对一个数组排序后打印,就有可能这样写:

a =[1,3,2,5,4]
print(a.sort())  #你会发现None
正确的写法:
a.sort()
print(a)


有意思,希望这样的帖子多一些



PyTypeObject _PyNone_Type = {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "NoneType",
    0,
    0,
    none_dealloc,       /*tp_dealloc*/ /*never called*/
    0,                  /*tp_vectorcall_offset*/
    0,                  /*tp_getattr*/
    0,                  /*tp_setattr*/
    0,                  /*tp_as_async*/
    none_repr,          /*tp_repr*/
    &none_as_number,    /*tp_as_number*/
    0,                  /*tp_as_sequence*/
    0,                  /*tp_as_mapping*/
    0,                  /*tp_hash */
    0,                  /*tp_call */
    0,                  /*tp_str */
    0,                  /*tp_getattro */
    0,                  /*tp_setattro */
    0,                  /*tp_as_buffer */
    Py_TPFLAGS_DEFAULT, /*tp_flags */
    0,                  /*tp_doc */
    0,                  /*tp_traverse */
    0,                  /*tp_clear */
    0,                  /*tp_richcompare */
    0,                  /*tp_weaklistoffset */
    0,                  /*tp_iter */
    0,                  /*tp_iternext */
    0,                  /*tp_methods */
    0,                  /*tp_members */
    0,                  /*tp_getset */
    0,                  /*tp_base */
    0,                  /*tp_dict */
    0,                  /*tp_descr_get */
    0,                  /*tp_descr_set */
    0,                  /*tp_dictoffset */
    0,                  /*tp_init */
    0,                  /*tp_alloc */
    none_new,           /*tp_new */
};

PyObject _Py_NoneStruct = {
  _PyObject_EXTRA_INIT
  1, &_PyNone_Type
};

HSB2 回复

python built_in 的 sort 是原地排序,并且会根据要排序的对象选择不同的算法来优化。

HSB2 回复

赞,没有想到这个场景

真·啥也不是

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册