偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python中的Null是什么?

開發(fā)
在計(jì)算機(jī)中null是一種類型,代表空字符,沒有與任何一個(gè)值綁定并且存儲(chǔ)空間也沒有存儲(chǔ)值。

在知乎上遇到一個(gè)問題,說:計(jì)算機(jī)中的「null」怎么讀?

圖片

null正確的發(fā)音是/n^l/,有點(diǎn)類似四聲‘納兒’,在計(jì)算機(jī)中null是一種類型,代表空字符,沒有與任何一個(gè)值綁定并且存儲(chǔ)空間也沒有存儲(chǔ)值。

Python中其實(shí)沒有null這個(gè)詞,取而代之的是None對(duì)象,即特殊類型NoneType,代表空、沒有。

None不能理解為0,因?yàn)?是有意義的,而None是一個(gè)特殊的空值:

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

None也不能理解為空字符'',因?yàn)榭兆址念愋褪亲址?/p>

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

雖然表示空,但None是一個(gè)具體的Python對(duì)象,這和null含義不一樣。

在Python中返回None:

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

你可以使用 Python 的標(biāo)識(shí)函數(shù)id()檢查 None 的唯一性,它返回某一對(duì)象的唯一標(biāo)識(shí)符,如果兩個(gè)變量的 id 相同,那么它們實(shí)際上指向的是同一個(gè)對(duì)象:

>>> 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的用處有很多,比如作為變量初始值、作為函數(shù)默認(rèn)參數(shù)、作為空值等等。

變量初始值:

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

函數(shù)默認(rèn)參數(shù):

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是一個(gè)對(duì)象,而null是一個(gè)類型。

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

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

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

責(zé)任編輯:趙寧寧 來源: Python大數(shù)據(jù)分析
相關(guān)推薦

2023-05-26 16:38:38

2022-03-17 05:42:05

__init__Python

2021-04-26 07:51:00

JavaScript方法函數(shù)

2010-06-29 13:58:17

SNMPMIB

2023-03-02 08:48:43

Linuxsubshell

2021-10-27 08:54:11

Pythonencodeencoding

2023-03-28 07:03:15

gRPCMetadata

2025-05-28 08:05:00

stdclassPHP開發(fā)

2022-09-26 12:46:02

Pythonelf

2022-06-29 08:37:03

事件循環(huán)JS 語言

2021-12-03 18:29:31

GoAny 泛型

2019-05-20 10:58:40

物聯(lián)網(wǎng)IOT技術(shù)

2022-07-28 08:34:59

事件委托JS

2023-03-01 09:49:23

2022-05-17 07:36:38

CSSBFC前端

2010-09-17 10:24:47

SQL中IS NULL

2023-10-11 08:29:54

volatileJava原子性

2021-06-11 10:33:14

MySQLPidSocket

2015-09-07 17:06:40

Swapfile.sy虛擬內(nèi)存Windows

2009-06-09 22:11:44

JavaScriptObject
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)