<p>你想看看<a href="http://diveintopython.net/power_of_introspection/built_in_functions.html" rel="noreferrer">^{<cd1>}</a>函数:</p>
<pre><code>>>> li = []
>>> dir(li)
['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']
</code></pre>
<blockquote>
<p><code>li</code> is a list, so <code>dir(li)</code> returns a list of all the methods of a list. Note that the returned list contains the names of the methods as strings, not the methods themselves. </p>
</blockquote>
<hr/>
<p><strong>根据评论进行编辑:</strong></p>
<p>不,这也将显示所有继承的方法。举个例子:</p>
<p><strong>测试.py:</strong></p>
<pre><code>class Foo:
def foo(): pass
class Bar(Foo):
def bar(): pass
</code></pre>
<p><strong>Python解释器:</strong></p>
<pre><code>>>> from test import Foo, Bar
>>> dir(Foo)
['__doc__', '__module__', 'foo']
>>> dir(Bar)
['__doc__', '__module__', 'bar', 'foo']
</code></pre>
<hr/>
<p><strong>您应该注意到</strong><a href="http://docs.python.org/library/functions.html" rel="noreferrer">Python's documentation</a>状态:</p>
<blockquote>
<p><strong>Note:</strong> Because <code>dir()</code> is supplied
<em>primarily as a convenience for use at
an interactive prompt</em>, it tries to
supply an interesting set of names
more than it tries to supply a
rigorously or consistently defined set
of names, <em>and its detailed behavior
may change across releases</em>. For
example, metaclass attributes are not
in the result list when the argument
is a class.</p>
</blockquote>
<p>因此在代码中使用是不安全的。改用<code>vars()</code>。<code>Vars()</code>不包括有关超类的信息,您必须自己收集它们。</p>
<hr/>
<p>如果使用<code>dir()</code>在交互式解释器中查找信息,请考虑使用<code>help()</code>。</p>