概述
從前面的對Python基礎(chǔ)知識方法介紹中,我們幾乎是圍繞Python內(nèi)置方法進行探索實踐,比如字符串、列表、字典等數(shù)據(jù)結(jié)構(gòu)的內(nèi)置方法,和大量內(nèi)置的標準庫,諸如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']
可以看到我們可以傳入某實例對象查看其屬性,也可以直接傳入其內(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__進行查看。
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
以上示例就實現(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)容會分成幾段,甚至可以進行交互展示對象的詳細信息。
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查看列表的所有詳細信息和屬性的用法等,通過回車鍵查看更多的信息。
官方中文文檔
對于英文閱讀有一定困難的小伙伴,新出Python官方中文文檔是較好的學(xué)習(xí)體驗教程:docs.python.org/zh-cn/3/,從入門教程,標準庫,在到Python高級特性應(yīng)有盡有,算是不錯的學(xué)習(xí)資源和一本常用的**“Python字典”**。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
