[Python-ideas] reprs of recursive datastructures.

Terry Reedy tjreedy at udel.edu
Fri Sep 7 23:57:31 CEST 2012


On 9/7/2012 3:51 PM, Mike Graham wrote:
> With the Python 3 loosening of where ... can occur, this somewhat
> suboptimal behaviour occurs
>
>>>> x = []
>>>> x.append(x)
>>>> x
> [[...]]
>>>> eval(repr(x))
> [[Ellipsis]]

I believe ... was used for representations before it became Ellipsis 
literal. In any case, the representation is now ambiguous. It is not 
possible to reliably invert a many-to-one function.

> Is this something that can be improved?

Change the recursive substitution so there is no ambiguity. For 
instance, use the unicode ellipsis character instead of '...', Since the 
output is unicode and may contain non-ascii chars anyway, that might be 
considered.
 >>> '\u2026'
'…'
 >>> [[...]]
[[Ellipsis]]
 >>> [[…]]
SyntaxError: invalid character in identifier

If not that, pick anything else giving a syntax error.
 >>> [[,,,]]
SyntaxError: invalid syntax
 >>> [[. . .]]
SyntaxError: invalid syntax

> Is it something worth improving?

I think so. Ambiguity is bad, and the substituted representation is 
something of a fib, so it should not mimic something that is valid. 
eval(representation of recursive structure) should either correctly 
evaluate by re-creating the recursive structure represented* or it 
should raise an error.

* That would mean that the same expression should be valid in code also. 
An interesting idea, and a deep can of worms. I believe it would require 
that '. . .' or whatever be recognizable syntax but not a named object, 
as the latter would re-introduce the same ambiguity.

-- 
Terry Jan Reedy





More information about the Python-ideas mailing list