Pythonic way to "flatten" object hierarchy to nested dicts?
<p>I need to "flatten" objects into nested dicts of the object's properties. The objects I want to do this with are generally just containers for basic types or other objects which act in a similar way. For example:</p>

<pre><code>class foo(object):
    bar = None
    baz = None

class spam(object):
    eggs = []

x = spam()
y = foo()
y.bar = True
y.baz = u"boz"
x.eggs.append(y)
</code></pre>

<p>What I need to "flatten" this to is:</p>

<pre><code>{ 'eggs': [ { 'bar': True, 'baz': u'boz' } ] }
</code></pre>

<p>Is there anything in the stdlib which can do this for me? If not, would I have to test <code>isinstance</code> against all known base-types to ensure I don't try to convert an object which can't be converted (eg: bool)?</p>

<p><strong>Edit:</strong></p>

<p>These are objects are being returned to my code from an external library and therefore I have no control over them. I could use them as-is in my methods, but it would be easier (safer?) to convert them to dicts - especially for unit testing.</p>
