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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:02:46+00:00 2026-06-14T00:02:46+00:00

Is there any standard convention to print an object in python. I know that

  • 0

Is there any standard convention to print an object in python. I know that if I just try to print the object it would print the memory address but I would like to overwrite that method and be able to print human readable format of the object to help in debugging.

is there any standard convention people follow or is it not a good way to define such a method instead there are better alternatives?

  • 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-14T00:02:47+00:00Added an answer on June 14, 2026 at 12:02 am

    You can overwrite either the __str__ or the __repr__ method.

    There is no convention on how to implement the __str__ method; it can just return any human-readable string representation you want. There is, however, a convention on how to implement the __repr__ method: it should return a string representation of the object so that the object could be recreated from that representation (if possible), i.e. eval(repr(obj)) == obj.

    Assuming you have a class Point, __str__ and __repr__ could be implemented like this:

    class Point:
        def __init__(self, x, y):
            self.x = x
            self.y = y
        def __str__(self):
            return "(%.2f, %.2f)" % (self.x, self.y)
        def __repr__(self):
            return "Point(x=%r, y=%r)" % (self.x, self.y)
        def __eq__(self, other):
            return isinstance(other, Point) and self.x == other.x and self.y == other.y
    

    Example:

    >>> p = Point(0.1234, 5.6789)
    >>> str(p)
    '(0.12, 5.68)'
    >>> "The point is %s" % p  # this will call str
    'The point is (0.12, 5.68)'
    >>> repr(p)
    'Point(x=0.1234, y=5.6789)'
    >>> p  # echoing p in the interactive shell calls repr internally
    Point(x=0.1234, y=5.6789)
    >>> eval(repr(p))  # this echos the repr of the new point created by eval
    Point(x=0.1234, y=5.6789)
    >>> type(eval(repr(p)))
    <class '__main__.Point'>
    >>> eval(repr(p)) == p
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any standard naming convention for VB.NET ? Based your programming experiences, would
I have a doubt: - Is there any standard/convention that when should I use
Is there any standard way of debugging Javascript on a webpage that's being accessed
Is there any way to start foobar.js WSH-script in order that standard Task Manager
There doesn't seem to be any sort of standard naming convention for OpenSSL certificates,
Is there such thing as standard naming convention for a class that interact with
Is there any standard way to route all Key events from the control A
Is there any standard way of implementing some sort of a write-through buffer for
is there any standard choice of the font family and font size for a
Is there any standard/documented ways to implement two-finger tap event in WP7? If not,

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.