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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:44:00+00:00 2026-05-28T07:44:00+00:00

Can I define a __repr__ for a class rather than an instance? For example,

  • 0

Can I define a __repr__ for a class rather than an instance? For example, I’m trying to do this

class A(object):
    @classmethod
    def __repr__(cls):
        return 'My class %s' % cls

What I get is

In [58]: a=A()

In [59]: a
Out[59]: My class <class '__main__.A'>

In [60]: A
Out[60]: __main__.A

I’m trying to get the output of line 60 to look like “My Class A”, not for the instance a. The reason I want to do this is I’m generating a lot of classes using Python’s metaclass. And I want a more readable way to identify the class than the stock repr.

  • 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-28T07:44:00+00:00Added an answer on May 28, 2026 at 7:44 am

    You need to define __repr__ on the metaclass.

    class Meta(type):
        def __repr__(cls):
            return 'My class %s' % cls.__name__
    
    class A(object):
        __metaclass__ = Meta
    

    __repr__ returns a representation of an instance of an object. So by defining __repr__ on A, you’re specifying what you want repr(A()) to look like.

    To define the representation of the class, you need to define how an instance of type is represented. In this case, replace type with a custom metaclass with __repr__ defined as you need.

    >> repr(A)
    My class A
    

    If you want to define a custom __repr__ for each class, I’m not sure there’s a particularly clean way to do it. But you could do something like this.

    class Meta(type):
        def __repr__(cls):
            if hasattr(cls, '_class_repr'):
                return getattr(cls, '_class_repr')()
            else:
                return super(Meta, cls).__repr__()
    
    class A(object):
        __metaclass__ = Meta
    
        @classmethod
        def _class_repr(cls):
            return 'My class %s' % cls.__name__
    
    class B(object):
        __metaclass__ = Meta
    

    Then you can customize on a per-class basis.

    >> repr(A)
    My class A
    >> repr(B)
    <__main__.B object at 0xb772068c>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can define an object and assign attributes and methods: class object: def __init__(self,a,b):
I can define default value in domain by this way : class ProcessingPriority {
I can define @some_ivar outside a class. To which class does this ivar belong
In Java you can define a new class inline using anonymous inner classes. This
In Java you can define generic class that accepts only types that extend a
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
In C# you can define delegates anonymously (even though they are nothing more than
I know that we can define two types linking each other, for instance: type
Is there anyway that i can define an NSString. Inside of one class then
We can define a constant like define(aconstant','avalue'); Can't we define array in this fashion

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.