擅长:python、mysql、java
<p>可以使用<code>dir()</code>或<code>__dict__</code>遍历对象的所有属性。您可以使用<code>isinstance()</code>和<a href="https://docs.python.org/2/library/types.html#types.FunctionType" rel="nofollow">^{<cd4>}</a>来判断哪些是函数。只要调用任何函数。</p>
<h2>更新</h2>
<p>正如Tadhg所评论的,<code>inspect.ismethod</code>似乎是最好的选择。下面是一些示例代码:</p>
<pre><code>import inspect
from itertools import ifilter
class Foo(object):
def foo1(self):
print('foo1')
def foo2(self):
print('foo2')
def foo3(self, required_arg):
print('foo3({!r})'.format(required_arg))
f = Foo()
attrs = (getattr(f, name) for name in dir(f))
methods = ifilter(inspect.ismethod, attrs)
for method in methods:
try:
method()
except TypeError:
# Can't handle methods with required arguments.
pass
</code></pre>