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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:48:06+00:00 2026-05-24T17:48:06+00:00

In writing a class recently, I initially included a __repr__ method along the following

  • 0

In writing a class recently, I initially included a __repr__ method along the following lines:

return "{}({!r}, {!r}, {!r})".format(
                self.__class__.__name__,
                self.arg1,
                self.arg2,
                self.arg3)

Repeating the ‘{!r}’ snippet like that feels wrong and would be tedious to maintain if I ever add any more arguments to this class. However, the more robust alternatives that occurred to me aren’t going to win any prizes for elegance either.

Building the format string programmatically:

fmt = "{}(%s)" % ", ".join(["{!r}"]*3)
return fmt.format(self.__class__.__name__,
                  self.arg1,
                  self.arg2,
                  self.arg3)

Formatting the arguments separately with str.join:

args = ", ".join(map(repr, [self.arg1, self.arg2, self.arg3]))
return "{}({})".format(self.__class__.__name__, args)

I’ve currently implemented the class using the last example, but I’m interested in suggestions for alternative approaches (since I’m not particularly happy with any of the above options).

Update:
Inspired by Ned’s answer, I’ve now added the following utility function to a helper module:

def format_iter(iterable, fmt='{!r}', sep=', '):
    return sep.join(fmt.format(x) for x in iterable)

>>> format_iter(range(10))
'0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
>>> format_iter(range(10), sep='|')
'0|1|2|3|4|5|6|7|8|9'
>>> format_iter(range(10), fmt='{:04b}', sep='|')
'0000|0001|0010|0011|0100|0101|0110|0111|1000|1001'
>>> format_iter(range(10), fmt='{0.real}+{0.imag}j')
'0+0j, 1+0j, 2+0j, 3+0j, 4+0j, 5+0j, 6+0j, 7+0j, 8+0j, 9+0j'

Update2:
I ended up adding a second utility function, almost identical to the one in agf’s answer:

def call_repr(name, *args):
    return "{}({})".format(name, format_iter(args))

So the originally offending __repr__ function now looks like:

def __repr__(self):
    return call_repr(self.__class__.__name__,
                     self.arg1,
                     self.arg2)

(Yes, one of the original constructor arguments went away earlier today.)

  • 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-05-24T17:48:07+00:00Added an answer on May 24, 2026 at 5:48 pm

    If this is a pattern you’re going to repeat, I’d probably use:

    # This is a static method or module-level function
    def argrepr(name, *args):
        return '{}({})'.format(name, ', '.join(repr(arg) for arg in args))
    
    def __repr__(self):
        return argrepr(self.__name__, self.arg1, self.arg2, self.arg3)
    

    or

    # This is a static method or module-level function
    def argrepr(*args):
        return '(' + ', '.join(repr(arg) for arg in args) + ')'
    
    def __repr__(self):
        return repr(self.__name__) + argrepr(self.arg1, self.arg2, self.arg3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I've begun writing my own PHP OpenID consumer class in order to better
Recently I've been writing code similar to this: messagehandler.h: #include message.h class MessageHandler {
I recently read that Java now sports initialisation blocks like the following: class C
When writing a class do you group members variables of the same type together?
I'm currently writing a class that implements the SeekableIterator interface and have run into
I'm busy writing a class that monitors the status of RAS connections. I need
I'm currently writing a class to handle all database-activity in my application, and so
I'm writing a class-library in VB.Net and one of the subs that are being
I am writing a class that is linked to an external resource. One of
I am writing a class that I know that needs a lock, because the

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.