avoiding recursion in repr?

Discussion in 'Python' started by David C. Fox, Nov 19, 2003.

  1. David C. Fox

    David C. Fox Guest

    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).

    David
     
    David C. Fox, Nov 19, 2003
    #1
    1. Advertising

  2. David C. Fox

    John J. Lee Guest

    "David C. Fox" <> 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
     
    John J. Lee, Nov 21, 2003
    #2
    1. Advertising

  3. David C. Fox

    David C. Fox Guest

    John J. Lee wrote:

    > "David C. Fox" <> 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"?


    For example an instance m of MyList containing a list l containing the
    same instance m of MyList...

    >
    > Couldn't you just use a dict to remember objects that have been seen,
    > like copy.deep_copy?


    No. deep_copy uses a memo argument (with a default value of None) to
    pass that dictionary around, but __repr__ for standard containers.
    Therefore, in the MyList example above, m.__repr__ can't pass such a
    dictionary when it calls the l.__repr__(), so when l.__repr__ calls
    m.__repr__ it doesn't get the extra argument. If I'm understanding the
    code for lists and dictionaries correctly, I think you need something
    global and thread safe (which is what Py_ReprEnter/Leave seem to be for).

    David
     
    David C. Fox, Dec 4, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmus Fogh

    multiline repr (code generation)

    Rasmus Fogh, Sep 3, 2003, in forum: Python
    Replies:
    0
    Views:
    365
    Rasmus Fogh
    Sep 3, 2003
  2. tcronj

    str() or repr()

    tcronj, Feb 12, 2004, in forum: Python
    Replies:
    0
    Views:
    370
    tcronj
    Feb 12, 2004
  3. Nikolai Kirsebom

    print / str / repr

    Nikolai Kirsebom, May 18, 2004, in forum: Python
    Replies:
    1
    Views:
    353
    vincent wehren
    May 18, 2004
  4. Replies:
    3
    Views:
    350
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    May 2, 2007
  5. Replies:
    8
    Views:
    761
    John Reye
    Apr 26, 2012
Loading...

Share This Page