[Python-ideas] reprs of recursive datastructures.

Nick Coghlan ncoghlan at gmail.com
Sat Sep 8 10:16:23 CEST 2012


On Sat, Sep 8, 2012 at 5:45 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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.

No, the problem is that you get the *wrong answer* instead of an exception.

Python 2:

>>> x = []
>>> x.append(x)
>>> x
[[...]]
>>> eval(repr(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    [[...]]
      ^
SyntaxError: invalid syntax

Python 3:

>>> x = []
>>> x.append(x)
>>> x
[[...]]
>>> eval(repr(x))
[[Ellipsis]]

As pointed out earlier, this is due to the fact that the previously
illegal notation used to indicate the recursive reference is now valid
syntax. The simplest fix is to just introduce alternative notation for
the self-reference that will reintroduce the desired syntax error,
such as "<...>" or "<self>".

Cheers,
Nick.
-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list