python利用dir函數(shù)查看類中所有成員函數(shù)示例代碼
前言
如果一個類是別人編寫的,又沒有幫助文檔,怎么樣來查看所有成員函數(shù)呢?本文詳細給大家介紹了關于python用dir函數(shù)查看類中所有成員函數(shù)的相關內(nèi)容,下面話不多說了,來一起看看詳細的介紹吧。
可以使用下面的代碼:
# File: builtin-dir-example-2.py
class A:
def a(self):
pass
def b(self):
pass
class B(A):
def c(self):
pass
def d(self):
pass
def getmembers(klass, members=None):
# get a list of all class members, ordered by class
if members is None:
members = []
for k in klass.__bases__:
getmembers(k, members)
for m in dir(klass):
if m not in members:
members.append(m)
return members
print('A=> :', getmembers(A))
print()
print('B=> :', getmembers(B))
print()
print('IOError=> :', getmembers(IOError))
輸出結果如下:
>>> ==== RESTART: D:/work/csdn/python_Game1/example/builtin-dir-example-2.py ==== A=> : ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__dict__', '__module__', '__weakref__', 'a', 'b'] B=> : ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__dict__', '__module__', '__weakref__', 'a', 'b', 'c', 'd'] IOError=> : ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__cause__', '__context__', '__dict__', '__setstate__', '__suppress_context__', '__traceback__', 'args', 'with_traceback', 'characters_written', 'errno', 'filename', 'filename2', 'strerror', 'winerror'] >>>
在這個例子里,輸出基類A的成員函數(shù),輸出派生類B的成員函數(shù)。
dir()內(nèi)置函數(shù)作用
python內(nèi)置方法有很多,無論是初學還是經(jīng)通python的程序員都不能全部記住所有方法,這時候dir()函數(shù)就非常有用了。使用dir()函數(shù)可以查看對像內(nèi)所有屬于及方法,在python中任何東西都是對像,一種數(shù)據(jù)類型,一個模塊等,都有自己的屬性和方法,除了常用方法外,其它的你不需要全部記住它,交給dir()函數(shù)就好了。
dir()函數(shù)使用方法
dir()函數(shù)操作方法很簡單,只需要把你想要查詢和對像添寫到( )括號中就可以使用了。
例如你想查看列表都有哪些方法,你可以在( )中直接傳入空列表對像[ ]或是一個列表數(shù)據(jù)類型的變量名,像下邊這樣操作:
>>>dir([ ])
或
x = ['a','b'] >>>dir(x)

兩種操作方法所得結果一樣,都是查看列表都有哪些操作方法及屬性的。如果你想查字符串,只要把()中的參數(shù)變量名或空字符串' '就可以了。
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
python 實現(xiàn)批量替換文本中的某部分內(nèi)容
今天小編就為大家分享一篇python 實現(xiàn)批量替換文本中的某部分內(nèi)容,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python報錯TypeError: ‘dict‘ object is not&
在Python開發(fā)的旅程中,報錯信息就像是一個個路障,阻礙著我們前進的步伐,而“TypeError: ‘dict’ object is not iterable”這個報錯,常常讓開發(fā)者們陷入困惑,那么,這個報錯究竟是怎么產(chǎn)生的呢?又該如何有效地解決它呢?讓我們一起深入探討,找到解決問題的方法2024-10-10
Python開發(fā)工具Pycharm的安裝以及使用步驟總結
今天給大家?guī)淼氖顷P于Python開發(fā)工具的安裝以及使用的相關知識,文章圍繞著如何使用和安裝Pycharm展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
基于python神經(jīng)卷積網(wǎng)絡的人臉識別
這篇文章主要為大家詳細介紹了基于python神經(jīng)卷積網(wǎng)絡的人臉識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
python利用opencv如何實現(xiàn)答題卡自動判卷
由于工作需要,最近在研究關于如何通過程序識別答題卡的客觀題的答案,所以下面這篇文章主要介紹了python利用opencv如何實現(xiàn)答題卡自動判卷的相關資料,需要的朋友可以參考下2021-08-08

