擅长:python、mysql、java
<p>在这个简单的例子中,您可以使用<a href="https://docs.python.org/2/library/functions.html#vars" rel="noreferrer" title="vars()">^{<cd1>}</a>:</p>
<pre><code>an = Animal()
attrs = vars(an)
# {'kids': 0, 'name': 'Dog', 'color': 'Spotted', 'age': 10, 'legs': 2, 'smell': 'Alot'}
# now dump this in some way or another
print(', '.join("%s: %s" % item for item in attrs.items()))
</code></pre>
<p>如果要在磁盘上存储Python对象,应该查看<a href="https://docs.python.org/2/library/shelve.html" rel="noreferrer" title="shelve — Python object persistence">shelve — Python object persistence</a>。</p>