擅长:python、mysql、java
<p>另一种方法是调用<a href="https://docs.python.org/2/library/functions.html#dir" rel="noreferrer" title="dir">^{<cd1>}</a>函数(请参见<a href="https://docs.python.org/2/library/functions.html#dir" rel="noreferrer" title="dir">https://docs.python.org/2/library/functions.html#dir</a>)。</p>
<pre><code>a = Animal()
dir(a)
>>>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'age', 'color', 'kids', 'legs', 'name', 'smell']
</code></pre>
<p>注意,<a href="https://docs.python.org/2/library/functions.html#dir" rel="noreferrer" title="dir">^{<cd1>}</a>尝试访问任何可能访问的属性。</p>
<p>然后您可以访问属性,例如使用双下划线进行筛选:</p>
<pre><code>attributes = [attr for attr in dir(a)
if not attr.startswith('__')]
</code></pre>
<p>这只是一个关于<a href="https://docs.python.org/2/library/functions.html#dir" rel="noreferrer" title="dir">^{<cd1>}</a>可以做什么的例子,请检查其他答案以了解正确的方法来做这件事。</p>