利用Python3内置文档资源高效学习及官方中文文档
概述
從前面的對(duì)Python基礎(chǔ)知識(shí)方法介紹中,我們幾乎是圍繞Python內(nèi)置方法進(jìn)行探索實(shí)踐,比如字符串、列表、字典等數(shù)據(jù)結(jié)構(gòu)的內(nèi)置方法,和大量?jī)?nèi)置的標(biāo)準(zhǔn)庫(kù),諸如functools、time、threading等等,而我們?cè)趺纯焖賹W(xué)習(xí)掌握并學(xué)會(huì)使用這個(gè)Python的工具集呢? 我們可以利用Python的內(nèi)置文檔大量資源既可以掌握許多關(guān)于Python工具集的基本使用。
dir函數(shù)
Python中內(nèi)置的dir函數(shù)用于提取某對(duì)象內(nèi)所有屬性的方法,,諸如對(duì)象的方法及屬性
L = [1, 2, 3, 4] print(dir(L)) print([]) 復(fù)制代碼示例結(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'] 復(fù)制代碼可以看到我們可以傳入某實(shí)例對(duì)象查看其屬性,也可以直接傳入其內(nèi)置類型的空對(duì)象查看對(duì)應(yīng)屬性,我們甚至還可以直接傳入類型的名稱得到對(duì)應(yīng)的屬性列表:
print(dir(list)) 復(fù)制代碼示例結(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'] 復(fù)制代碼雖然我們獲得了對(duì)象的屬性,但我們?nèi)匀徊恢肋@些屬性方法的含義,那么我們可以利用文檔字符串幫助我們繼續(xù)學(xué)習(xí)對(duì)象屬性。
文檔字符串:doc
文檔字符串是由Python自動(dòng)生成的,而生成的內(nèi)內(nèi)容和位置取決于我們的放置方式,文檔字符串也是一段注釋,放在模塊文件、函數(shù)以及類語(yǔ)句的頂端,然后Python會(huì)自動(dòng)封裝這個(gè)字符串,即成為所謂的文檔字符串,通過(guò)對(duì)象的__doc__進(jìn)行查看。
def two_sum(x, y):'''Used to calculate the sum of two numbers'''return x + yprint(two_sum.__doc__) 復(fù)制代碼示例結(jié)果:
Used to calculate the sum of two numbers 復(fù)制代碼以上示例就實(shí)現(xiàn)了對(duì)一個(gè)函數(shù)(用于計(jì)算兩數(shù)之和)綁定文檔字符串并查看其文檔字符串的過(guò)程。我們也可以查看一些內(nèi)置類型的某屬性的具體使用方法,比如查看列表對(duì)象中pop的具體含義和用法
L = [1, 2, 3, 4] print(L.pop.__doc__) 復(fù)制代碼示例結(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. 復(fù)制代碼PyDoc:help函數(shù)
我們可以利用Python中help函數(shù)工具更加友好結(jié)構(gòu)化的展示對(duì)象的文檔字符串和其他的信息,對(duì)于對(duì)于某些較大的對(duì)象help內(nèi)容會(huì)分成幾段,甚至可以進(jìn)行交互展示對(duì)象的詳細(xì)信息。
help(list) 復(fù)制代碼交互結(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 -- 復(fù)制代碼比如我們可以通過(guò)help查看列表的所有詳細(xì)信息和屬性的用法等,通過(guò)回車鍵查看更多的信息。
官方中文文檔
對(duì)于英文閱讀有一定困難的小伙伴,新出Python官方中文文檔是較好的學(xué)習(xí)體驗(yàn)教程:docs.python.org/zh-cn/3/,從入門教程,標(biāo)準(zhǔn)庫(kù),在到Python高級(jí)特性應(yīng)有盡有,算是不錯(cuò)的學(xué)習(xí)資源和一本常用的**“Python字典”**。
轉(zhuǎn)載于:https://juejin.im/post/5ce0244e51882525be132c76
總結(jié)
以上是生活随笔為你收集整理的利用Python3内置文档资源高效学习及官方中文文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 各种文档
- 下一篇: python利用unittest进行测试