利用Python3内置文档资源高效学习及官方中文文档
概述
從前面的對Python基礎(chǔ)知識方法介紹中,我們幾乎是圍繞Python內(nèi)置方法進(jìn)行探索實踐,比如字符串、列表、字典等數(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([]) 復(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ù)制代碼可以看到我們可以傳入某實例對象查看其屬性,也可以直接傳入其內(nèi)置類型的空對象查看對應(yīng)屬性,我們甚至還可以直接傳入類型的名稱得到對應(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ù)制代碼雖然我們獲得了對象的屬性,但我們?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 + yprint(two_sum.__doc__) 復(fù)制代碼示例結(jié)果:
Used to calculate the sum of two numbers 復(fù)制代碼以上示例就實現(xiàn)了對一個函數(shù)(用于計算兩數(shù)之和)綁定文檔字符串并查看其文檔字符串的過程。我們也可以查看一些內(nèi)置類型的某屬性的具體使用方法,比如查看列表對象中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)化的展示對象的文檔字符串和其他的信息,對于對于某些較大的對象help內(nèi)容會分成幾段,甚至可以進(jìn)行交互展示對象的詳細(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ù)制代碼比如我們可以通過help查看列表的所有詳細(xì)信息和屬性的用法等,通過回車鍵查看更多的信息。
官方中文文檔
對于英文閱讀有一定困難的小伙伴,新出Python官方中文文檔是較好的學(xué)習(xí)體驗教程:docs.python.org/zh-cn/3/,從入門教程,標(biāo)準(zhǔn)庫,在到Python高級特性應(yīng)有盡有,算是不錯的學(xué)習(xí)資源和一本常用的**“Python字典”**。
轉(zhuǎn)載于:https://juejin.im/post/5ce0244e51882525be132c76
總結(jié)
以上是生活随笔為你收集整理的利用Python3内置文档资源高效学习及官方中文文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。