object to string in Python : <p>I have some data objects on which I want to implement a to string and equals functions that go in depth. </p>

<p>I implemented <strong>str</strong> and <strong>eq</strong> and although equality works fine I cannot make <strong>str</strong> behave in the same way:</p>

<pre><code>class Bean(object):

    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2

    def __str__(self):
        return str(self.__dict__)

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
</code></pre>

<p>When I run:</p>

<pre><code>t1 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)])
t2 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)])
t3 = Bean("bean 1", [Bean("bean 1.1", "different"), Bean("bean 1.2", 42)])

print(t1)
print(t2)
print(t3)
print(t1 == t2)
print(t1 == t3)
</code></pre>

<p>I get:</p>

<pre><code>{'attr2': [&lt;__main__.Bean object at 0x7fc092030f28&gt;, &lt;__main__.Bean object at 0x7fc092030f60&gt;], 'attr1': 'bean 1'}
{'attr2': [&lt;__main__.Bean object at 0x7fc091faa588&gt;, &lt;__main__.Bean object at 0x7fc092045128&gt;], 'attr1': 'bean 1'}
{'attr2': [&lt;__main__.Bean object at 0x7fc0920355c0&gt;, &lt;__main__.Bean object at 0x7fc092035668&gt;], 'attr1': 'bean 1'}
True
False
</code></pre>

<p>since t1 and t2 contain the same values the equals return true (as expected) while since t3 contains a different value in the list the result is false (also as expected).
What I would like is to have the same behavior for the to string (basically to also go in depth also for the elements in list (or set or dict ...).</p>

<p>For print(t1) I would like to obtain something like:</p>

<pre><code>{'attr2': ["{'attr2': 'same', 'attr1': 'bean 1.1'}", "{'attr2': 42, 'attr1': 'bean 1.2'}"], 'attr1': 'bean 1'}
</code></pre>

<p>which is actually obtained if I do:</p>

<pre><code>Bean("bean 1", [Bean("bean 1.1", "same").__str__(), Bean("bean 1.2", 42).__str__()]).__str__
</code></pre>

<p>Since I do not know the types of the attributes attr1, attr2 in my Bean objects (they may be lists but also sets, dictionaries etc.) is would be nice to have a simple and elegant solution that would not require type checking ...</p>

<p>Is this possible ?</p>
