亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Python中的null是什么

 更新時間:2022年09月15日 09:10:35   作者:Python大數(shù)據(jù)分析  
這篇文章主要介紹了Python中的null是什么,Python中其實沒有null這個詞,取而代之的是None對象,即特殊類型NoneType,代表空、沒有,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

在知乎上遇到一個問題,說:計算機中的「null」怎么讀?

null正確的發(fā)音是/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 的標(biāo)識函數(shù) id() 檢查 None 的唯一性,它返回某一對象的唯一標(biāo)識符,如果兩個變量的 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的用處有很多,比如作為變量初始值、作為函數(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是一個對象,而null是一個類型。

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

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

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

到此這篇關(guān)于Python中的null是什么?的文章就介紹到這了,更多相關(guān)Python null內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論