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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:30:48+00:00 2026-05-23T03:30:48+00:00

It seems there are different ways the __repr__ function can return. I have a

  • 0

It seems there are different ways the __repr__ function can return.

I have a class InfoObj that stores a number of things, some of which I don’t particularly want users of the class to set by themselves. I recognize nothing is protected in python and they could just dive in and set it anyway, but seems defining it in __init__ makes it more likely someone might see it and assume it’s fine to just pass it in.

(Example: Booleans that get set by a validation function when it determines that the object has been fully populated, and values that get calculated from other values when enough information is stored to do so… e.g. A = B + C, so once A and B are set then C is calculated and the object is marked Valid=True.)

So, given all that, which is the best way to design the output of __ repr__?

    bob = InfoObj(Name="Bob")
    # Populate bob.

    # Output type A:
    bob.__repr__()
'<InfoObj object at 0x1b91ca42>'

    # Output type B:
    bob.__repr__()
'InfoObj(Name="Bob",Pants=True,A=7,B=5,C=2,Valid=True)'

    # Output type C:
    bob.__repr__()
'InfoObj.NewInfoObj(Name="Bob",Pants=True,A=7,B=5,C=2,Valid=True)'

… the point of type C would be to not happily take all the stuff I’d set ‘private’ in C++ as arguments to the constructor, and make teammates using the class set it up using the interface functions even if it’s more work for them. In that case I would define a constructor that does not take certain things in, and a separate function that’s slightly harder to notice, for the purposes of __repr__

If it makes any difference, I am planning to store these python objects in a database using their __repr__ output and retrieve them using eval(), at least unless I come up with a better way. The consequence of a teammate creating a full object manually instead of going through the proper interface functions is just that one type of info retrieval might be unstable until someone figures out what he did.

  • 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-23T03:30:48+00:00Added an answer on May 23, 2026 at 3:30 am

    The __repr__ method is designed to produce the most useful output for the developer, not the enduser, so only you can really answer this question. However, I’d typically go with option B. Option A isn’t very useful, and option C is needlessly verbose — you don’t know how your module is imported anyway. Others may prefer option C.

    However, if you want to store Python objects is a database, use pickle.

    import pickle
    bob = InfoObj(Name="Bob")
    
    > pickle.dumps(bob)
    b'...some bytestring representation of Bob...'
    
    > pickle.loads(pickle.dumps(bob))
    Bob(...)
    

    If you’re using older Python (pre-3.x), then note that cPickle is faster, but pickle is more extensible. Pickle will work on some of your classes without any configuration, but for more complicated objects you might want to write custom picklers.

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

Sidebar

Related Questions

I've been looking at different ways of creating a certain physics simulation. What I
I want to give access to protected properties to another class. I have been
I need to create an activity that starts a service that monitors the users
Please understand that I'm not trying to bash RVM. I would just like to
I have a a game for the iphone where the tags of 32 buttons
I'm in the process of writing an image script for a website I'm making,
I wanted to start using attr_accessible with my models to stop the problem with
What are the pros and cons of standardizing on using Option Compare Text vs
EDIT: LAST UPDATE : So, we've tested some more. I used the facebook developers

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.