如何列舉 Python 類的函式?


以下程式碼列印給定類的函式列表,如下所示:

示例

class foo:
    def __init__(self):
        self.x = x     def bar(self):
        pass
    def baz(self):
        pass
print (type(foo))
import inspect
print(inspect.getmembers(foo, predicate=inspect.ismethod))

輸出

輸出為 

<type 'classobj'>
[('__init__', <unbound method foo.__init__>), ('bar', <unbound method foo.bar>), ('baz', <unbound method foo.baz>)]

  

 

更新於: 15-6-2020

177 瀏覽量

開啟你的職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.