Python中的null是什么?

在知乎上遇到一个问题,说:计算机中的「null」怎么读?

null正确的发音是/n^l/,有点类似四声‘纳儿’,在计算机中null是一种类型,代表空字符,没有与任何一个值绑定并且存储空间也没有存储值。

Python中其实没有null这个词,取而代之的是None对象,即特殊类型NoneType,代表空、没有。

None不能理解为0,因为0是有意义的,而None是一个特殊的空值。

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType

None也不能理解为空字符’',因为空字符的类型是字符串。

>>>type('')
<class ''str'>

虽然表示空,但None是一个具体的Python对象,这和null含义不一样。

在Python中返回None:

>>> def has_no_return():
...     pass
>>> has_no_return()
>>> print(has_no_return())
None

你可以使用 Python 的标识函数id()检查 None 的唯一性,它返回某一对象的唯一标识符,如果两个变量的 id 相同,那么它们实际上指向的是同一个对象。

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000

在Python中,None的用处有很多,比如作为变量初始值、作为函数默认参数、作为空值等等。

变量初始值

>>> print(bar)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined
>>> bar = None
>>> print(bar)
None

函数默认参数

def bad_function(new_elem, starter_list=[]):
    starter_list.append(new_elem)
    return starter_list

空值

>>> class DontAppend: pass
...
>>> def good_function(new_elem=DontAppend, starter_list=None):
...     if starter_list is None:
...         starter_list = []
...     if new_elem is not DontAppend:
...         starter_list.append(new_elem)
...     return starter_list
...
>>> good_function(starter_list=my_list)
['a', 'b', 'c', 'd', 'e']
>>> good_function(None, my_list)
['a', 'b', 'c', 'd', 'e', None]

总得来说,None是一个对象,而null是一个类型。

Python中沒有null,只有None,None有自己的特殊类型NoneType。

None不等于0、任何空字符串、False等。

在Python中,None、False、0、””(空字符串)、、()(空元組)、{}(空字典)都相当于False。

相关推荐

  1. MySQLNULL和空区别什么?底层原理什么

    2024-07-12 06:44:03       39 阅读
  2. python multiprocessing.Event什么

    2024-07-12 06:44:03       57 阅读
  3. Python什么

    2024-07-12 06:44:03       60 阅读
  4. Python变量什么类型?

    2024-07-12 06:44:03       37 阅读
  5. 什么 Python __pycache__ 文件夹?

    2024-07-12 06:44:03       25 阅读
  6. Python生成器什么

    2024-07-12 06:44:03       28 阅读
  7. Python列表什么

    2024-07-12 06:44:03       24 阅读
  8. pythonyield什么

    2024-07-12 06:44:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 06:44:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 06:44:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 06:44:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 06:44:03       69 阅读

热门阅读

  1. MybatisPlus 一些技巧

    2024-07-12 06:44:03       23 阅读
  2. 云计算练习题

    2024-07-12 06:44:03       23 阅读
  3. 怎么在ad原理图中替换器件

    2024-07-12 06:44:03       22 阅读
  4. 目标识别步骤

    2024-07-12 06:44:03       24 阅读
  5. vs QT Use QGuiApplication::screens报错

    2024-07-12 06:44:03       25 阅读
  6. 【qml学习笔记】QML与C++的交互

    2024-07-12 06:44:03       28 阅读
  7. stm32使用串口打印

    2024-07-12 06:44:03       21 阅读
  8. Windows驱动开发

    2024-07-12 06:44:03       26 阅读
  9. [linux] git push时需要输入user 和keyword

    2024-07-12 06:44:03       21 阅读
  10. 【AIGC】GPT-4深度解析:自然语言处理的新纪元

    2024-07-12 06:44:03       22 阅读
  11. PyTorch中的CPU和GPU代码实现详解

    2024-07-12 06:44:03       25 阅读
  12. CSS实现从上往下过渡效果

    2024-07-12 06:44:03       28 阅读