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 8851055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:03:11+00:00 2026-06-14T13:03:11+00:00

It bugs me that the default __repr__() for a class is so uninformative: >>>

  • 0

It bugs me that the default __repr__() for a class is so uninformative:

>>> class Opaque(object): pass
... 
>>> Opaque()
<__main__.Opaque object at 0x7f3ac50eba90>

… so I’ve been thinking about how to improve it. After a little consideration, I came up with this abstract base class which leverages the pickle protocol’s __getnewargs__() method:

from abc import abstractmethod

class Repro(object):

    """Abstract base class for objects with informative ``repr()`` behaviour."""

    @abstractmethod
    def __getnewargs__(self):
        raise NotImplementedError

    def __repr__(self):
        signature = ", ".join(repr(arg) for arg in self.__getnewargs__())
        return "%s(%s)" % (self.__class__.__name__, signature)

Here’s a trivial example of its usage:

class Transparent(Repro):

    """An example of a ``Repro`` subclass."""

    def __init__(self, *args):
        self.args = args

    def __getnewargs__(self):
        return self.args

… and the resulting repr() behaviour:

>>> Transparent("an absurd signature", [1, 2, 3], str)
Transparent('an absurd signature', [1, 2, 3], <type 'str'>)
>>> 

Now, I can see one reason Python doesn’t do this by default straight away – requiring every class to define a __getnewargs__() method would be more burdensome than expecting (but not requiring) that it defines a __repr__() method.

What I’d like to know is: how dangerous and/or fragile is it? Off-hand, I can’t think of anything that could go terribly wrong except that if a Repro instance contained itself, you’d get infinite recursion … but that’s solveable, at the cost of making the code above uglier.

What else have I missed?

  • 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-14T13:03:13+00:00Added an answer on June 14, 2026 at 1:03 pm

    One problem with this whole idea is that there can be some kinds of objects who’s state is not fully dependent on the arguments given to its constructor. For a trivial case, consider a class with random state:

    import random
    
    def A(object):
        def __init__(self):
            self.state = random.random()
    

    There’s no way for this class to correctly implement __getnewargs__, and so your implantation of __repr__ is also impossible. It may be that a class like the one above is not well designed. But pickle can handle it with no problems (I assume using the __reduce__ method inherited from object, but my pickle-fu is not enough to say so with certainty).

    This is why it is nice that __repr__ can be coded to do whatever you want. If you want the internal state to be visible, you can make your class’s __repr__ do that. If the object should be opaque, you can do that too. For the class above, I’d probably implement __repr__ like this:

    def __repr__(self):
        return "<A object with state=%f>" % self.state
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using Xcode for a while now. One thing that always bugs me
I recently discovered that relying on default encoding of JVM causes bugs. I should
When throwing exceptions, I often pass in a formatted string that exposes details about
It bugs me that I can't just do document.querySelectorAll(...).map(...) even in Firefox 3.6, and
It bugs me to death that my viewcontroller, which happens to be a tableViewController,
I have recently discovered that I am affected by this bug http://www.mail-archive.com/mono-bugs@lists.ximian.com/msg71515.html Well, at
This question bugs me. How event handling systems works? what i understand is that
On a page of Java's Bug Database https://bugs.java.com/bugdatabase/view_bug?bug_id=4508058 it reads that Sun/Oracle will not
Findbugs bugs me about a method which opens two Closeable instances, but I can't
DB Designer had a lot of bugs but one outstanding feature was that database

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.