如何列举 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>)]
广告