Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:43:22+00:00 2026-06-08T18:43:22+00:00

A function recursive_repr in reprlib module, introduced in Python 3.2, has the following source

  • 0

A function recursive_repr in reprlib module, introduced in Python 3.2, has the following source code:

def recursive_repr(fillvalue='...'):
    'Decorator to make a repr function return fillvalue for a recursive call'

    def decorating_function(user_function):
        repr_running = set()

        def wrapper(self):
            key = id(self), get_ident()
            if key in repr_running:
                return fillvalue
            repr_running.add(key)
            try:
                result = user_function(self)
            finally:
                repr_running.discard(key)
            return result

        # Can't use functools.wraps() here because of bootstrap issues
        wrapper.__module__ = getattr(user_function, '__module__')
        wrapper.__doc__ = getattr(user_function, '__doc__')
        wrapper.__name__ = getattr(user_function, '__name__')
        wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
        return wrapper

    return decorating_function

The key identifying the specific __repr__ function is set to be (id(self), get_ident()).

Why self itself wasn’t used as a key?
And why get_ident was needed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:43:23+00:00Added an answer on June 8, 2026 at 6:43 pm

    Consider this code:

    a = []
    b = []
    a.append(b)
    b.append(a)
    a == b
    

    This causes a stack overflow. But the algorithm needs to handle this case safely. If you put self into the set, it’ll compare using == and the algorithm will fail. So we use id(self) instead which doesn’t attempt to check for equality of the objects. We only care if it is the exact same object.

    As for get_indent, consider what happens in this algorithm if two threads try to use the code at the same time. The repr_running set is shared between all the threads. But if multiple thread start adding and removing elements from that set, there is no telling what will happen. get_ident() is unique to the thread running it, so by using that with the key we know all threads will use different keys and be ok.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python 3.2 introduces a new function recursive_repr in reprlib module. When I looked into
Recursive function for copy of multilevel folder is not working. I have a code
I have the following recursive function for project euler question no. 74 : chain
I have a recursive function which contains some drawing code inside. I was advised
I am having recursive function. it make calls every second. I want to kill
Given the following recursive function: // Pre-condition: y is non-negative. int mysterious(int x, int
Given the following recursive function, what would be printed by mysterious(4)? void mysterious(int x)
To make the process of recursion more visible, this example is given : def
I have recursive function which walk nested dict and return needed key's value: def
Why does this recursive function keep returning multiple dictionaries instead of just one. def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.