[Python-ideas] reprs of recursive datastructures.

Steven D'Aprano steve at pearwood.info
Sat Sep 8 09:45:54 CEST 2012

On 08/09/12 16:27, Guido van Rossum wrote:
> Can someone explain what problem we are trying to solve? I fail to
> uderstand what's wrong with the current behavior...

I believe that some people think that if you eval the repr of a
recursive list, the result should be an equally recursive list. But
it isn't:

py> x = [1, 2, 3]
py> x.append(x)
py> eval(repr(x)) == x

I think they are misguided in their expectation. There is no way
to write a single expression using list literals which generates
a recursive list, so why would you expect eval to produce one?

Furthermore, list reprs of recursive lists have been ambiguous
for years. This code works identically in 2.4 and 3.2:

py> a = []; a.append(a)
py> b = []; b.append(b)
py> x = [[], []]; x[0].append(x); x[1].append(x)
py> y = [a, b]
py> x == y
py> repr(x) == repr(y)

eval(repr(x)) == x is not a guaranteed invariant, it is a "nice
to have". -1 on trying to fix this.


More information about the Python-ideas mailing list