How do I handle recursive repr()'s in Python?
<p>I've written a container type in Python and I'm trying to write a robust <code>__repr__</code> method that correctly handles the case where the container contains itself.</p>

<p>For example, here's what the built-in <code>list</code> does:</p>

<pre><code>&gt;&gt;&gt; x = []
&gt;&gt;&gt; x.append(x)
&gt;&gt;&gt; repr(x)
'[[...]]'
</code></pre>

<p>Container types written in C for CPython can achieve this functionality by using <code>Py_ReprEnter</code> and <code>Py_ReprLeave</code>.  Is there equivalent functionality in pure-Python, or do I need to create my own?</p>
