avoiding recursion in repr?
John J. Lee
jjl at pobox.com
Thu Nov 20 19:57:51 EST 2003
"David C. Fox" <davidcfox at post.harvard.edu> writes:
> The __repr__ methods of the built-in list and dict objects properly
> avoid an infinite loop when you take the representation of a list or
> dictionary which contains itself (or more complicated nestings: list
> l1 contains list l2 which contains l1, etc.).
>
> Looking at the CPython source code, it seems that the Py_ReprEnter and
> Py_ReprLeave functions are used to implement this protection.
>
> Is there any equivalent function for defining new container classes
> written in Python, or do potentially recursive containers have to be
> implemented in C?
>
> (I could of course try to translate these functions into Python, but
> I'm not sure that would catch mixed nestings of my class and
> list/dict).
What's a "mixed nesting"?
Couldn't you just use a dict to remember objects that have been seen,
like copy.deep_copy?
John
More information about the Python-list
mailing list