溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Python3內(nèi)置文檔高效學(xué)習(xí)以及官方中文文檔

發(fā)布時間:2020-10-15 13:42:31 來源:腳本之家 閱讀:144 作者:若數(shù) 欄目:開發(fā)技術(shù)

概述

從前面的對Python基礎(chǔ)知識方法介紹中,我們幾乎是圍繞Python內(nèi)置方法進(jìn)行探索實(shí)踐,比如字符串、列表、字典等數(shù)據(jù)結(jié)構(gòu)的內(nèi)置方法,和大量內(nèi)置的標(biāo)準(zhǔn)庫,諸如functools、time、threading等等,而我們怎么快速學(xué)習(xí)掌握并學(xué)會使用這個Python的工具集呢? 我們可以利用Python的內(nèi)置文檔大量資源既可以掌握許多關(guān)于Python工具集的基本使用。

dir函數(shù)

Python中內(nèi)置的dir函數(shù)用于提取某對象內(nèi)所有屬性的方法,,諸如對象的方法及屬性

L = [1, 2, 3, 4]
print(dir(L))
print([])

示例結(jié)果:

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

可以看到我們可以傳入某實(shí)例對象查看其屬性,也可以直接傳入其內(nèi)置類型的空對象查看對應(yīng)屬性,我們甚至還可以直接傳入類型的名稱得到對應(yīng)的屬性列表:

print(dir(list))

示例結(jié)果:

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

雖然我們獲得了對象的屬性,但我們?nèi)匀徊恢肋@些屬性方法的含義,那么我們可以利用文檔字符串幫助我們繼續(xù)學(xué)習(xí)對象屬性。

文檔字符串:doc

文檔字符串是由Python自動生成的,而生成的內(nèi)內(nèi)容和位置取決于我們的放置方式,文檔字符串也是一段注釋,放在模塊文件、函數(shù)以及類語句的頂端,然后Python會自動封裝這個字符串,即成為所謂的文檔字符串,通過對象的__doc__進(jìn)行查看。

def two_sum(x, y):
 '''
 Used to calculate the sum of two numbers
 '''
 return x + y


print(two_sum.__doc__)

示例結(jié)果:

Used to calculate the sum of two numbers

以上示例就實(shí)現(xiàn)了對一個函數(shù)(用于計算兩數(shù)之和)綁定文檔字符串并查看其文檔字符串的過程。我們也可以查看一些內(nèi)置類型的某屬性的具體使用方法,比如查看列表對象中pop的具體含義和用法

L = [1, 2, 3, 4]
print(L.pop.__doc__)

示例結(jié)果:

L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.

PyDoc:help函數(shù)

我們可以利用Python中help函數(shù)工具更加友好結(jié)構(gòu)化的展示對象的文檔字符串和其他的信息,對于對于某些較大的對象help內(nèi)容會分成幾段,甚至可以進(jìn)行交互展示對象的詳細(xì)信息。

help(list)

交互結(jié)果:

Help on class list in module __builtin__:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |
 |  Methods defined here:
 |
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |
 |  __delslice__(...)
 |      x.__delslice__(i, j) <==> del x[i:j]
 |
-- More  --

比如我們可以通過help查看列表的所有詳細(xì)信息和屬性的用法等,通過回車鍵查看更多的信息。

官方中文文檔

對于英文閱讀有一定困難的小伙伴,新出Python官方中文文檔是較好的學(xué)習(xí)體驗(yàn)教程:docs.python.org/zh-cn/3/,從入門教程,標(biāo)準(zhǔn)庫,在到Python高級特性應(yīng)有盡有,算是不錯的學(xué)習(xí)資源和一本常用的**“Python字典”**。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI