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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:45:11+00:00 2026-05-29T11:45:11+00:00

I understand the following Python code: >>> class A(object): … def __str__(self): … return

  • 0

I understand the following Python code:

>>> class A(object):
...     def __str__(self):
...         return "An instance of the class A"
... 
>>> 
>>> a = A()
>>> print a
An instance of the class A

Now, I would like to change the output of

>>> print A
<class '__main__.A'>

Which function do I need to overload to be able to do that? The solution has to work even if the class is never instantiated. Is the situation different in Python 2.x and 3?

  • 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-29T11:45:11+00:00Added an answer on May 29, 2026 at 11:45 am

    Define __str__() on the metaclass:

    class A(object):
        class __metaclass__(type):
            def __str__(self):
                return "plonk"
    

    Now, print A will print plonk.

    Edit: As noted by jsbueno in the comments, in Python 3.x you would need to do the following:

    class Meta(type):
        def __str__(self):
            return "plonk"
    class A(metaclass=Meta):
        pass
    

    Even in Python 2.x it might be a better idea to define the metaclass outside the class body — I chose the nested form above to save some typing.

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

Sidebar

Related Questions

The following code: #!/usr/bin/env python import mechanize class MechanizeSubclass(mechanize.Browser): def __init__(self, factory=None, history=None, request_class=None,
the code here below: #!/usr/bin/env python import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None,
I dont understand what would be the problem with the following code. It needs
I'm trying to understand if the following Python function: def factorial(i): if not hasattr(factorial,
In the following Python code, I get an UnboundLocalError . As I understand it,
Could someone help me understand what is going on in the following Python code
As of now I use the following python code: file = open(filePath, r) lines=file.readlines()
I'm exploring the Python multiprocessing module and don't understand why the following code does
I have the following code (as an example) in a unittest TestCase def test(self):
I have the following code in Python: for i in range(4): if self.start ==

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.