I've written a container type in Python and I'm trying to write a robust __repr__
method that correctly handles the case where the container contains itself.
For example, here's what the built-in list
does:
>>> x = []
>>> x.append(x)
>>> repr(x)
'[[...]]'
Container types written in C for CPython can achieve this functionality by using Py_ReprEnter
and Py_ReprLeave
. Is there equivalent functionality in pure-Python, or do I need to create my own?