[Python-Dev] Trashing recursive objects comparison?

Guido van Rossum guido at python.org
Fri Oct 17 10:46:31 EDT 2003

> I'm bringing (again) the subject of comparison of recursive objects to the 
> table because I just happened to write a buggy piece of code:
> class X:
>    def __eq__(self, other):
>       return self.content == other
> This code was buggy because 'self.content' could occasionally be 'self'. In
> this case it should have triggered an infinite recursion, and I should have
> got a nice (if a bit long) RuntimeError traceback that told me where the
> problem was. At least, this is how I would expect my piece of code to
> misbehave.
> Instead, the answer was 'True', whatever 'other' actually was. Puzzlement
> would have gained me if I had no idea about what a bisimulation, or graph
> isomorphism, is, and what Python's implementation of that idea is.
> Quoting Tim on bug #625698:
> > As Erik's latest example shows, the outcome isn't always 
> > particularly well defined either.  An alternative to speeding
> > this
> > silliness is to raise an exception instead when recursive 
> > objects are detected.  There was some hack value in doing 
> > the graph isomorphism bit, but no real practical value I can 
> > see.
> If the pretty academic subject of graph isomorphisms is well-worn
> enough to be sent to the trash, I'll submit a patch that just
> removes all this code and instead use the existing
> sys.recursionlimit counter to catch infinite recursions and throw
> the usual RuntimeError.

Go for it, Armin.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list