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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:26:50+00:00 2026-06-14T07:26:50+00:00

I have a class with __str__ and __repr__ defined: class MyClass: def __init__(self, version):

  • 0

I have a class with __str__ and __repr__ defined:

class MyClass:
    def __init__(self, version):
        self.version = version
    def __repr__(self):
        return self.version

myObjs = {}
o = MyClass("1")
myObjs[o] = o
print myObjs

Which prints:

{1: 1}

How do I store the MyClass object as value to the dictionary instead of the string representation?
I could have added getVersion(), but just wanted to know if there is some more elegant way.

Thanks

  • 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-14T07:26:51+00:00Added an answer on June 14, 2026 at 7:26 am

    As mentioned in the comments, the dictionary is storing the object.

    class MyClass(object):
        def __init__(self, version):
            self.version = version
        def __repr__(self):
            return self.version
    
    myObjs = {}
    o = MyClass("1")
    myObjs[o] = o
    print type(o)                                                                   
    print o.__class__                                                               
    print myObjs                                                                    
    print type(myObjs[o])
    

    Prints the following:

    <class '__main__.MyClass'>
    <class '__main__.MyClass'>
    {1: 1}
    <class '__main__.MyClass'>
    

    The reason it prints this way is because the dictionary uses the __repr__ of its keys and values for both its __str__ and __repr__ functions. You can always change your __repr__ function though to make it less ambiguous with an integer:

    def __repr__(self):
        return '{0}({1})'.format(str(self.__class__.__name__), self.version)
    

    Sample Output:

    {MyClass(1): MyClass(1)}
    

    Feel free to use whichever format that works best for you. I also changed your class to extend from object to make it a new style class (it brings classes and types together in the language and adds additional abilities to classes (such as decorators and the super function)). Otherwise:

    print type(o)
    

    Outputs

    <type 'instance'>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an exception class defined #####UNIQUE CONSTRAINT EXCEPTION#########################################################3 class UniqueConstraintException (Exception): def __init__(self,
I have the following class: class StrLogger(str): def __init__(self, *args): self._log_ = [] str.__init__(self,
I have a class Commit . class Commit: def __init__(self, uid, message): self.uid =
I have a class called Cell: class Cell: def __init__(self, value, color, size): self._value
I have written a class in python that implements __str__(self) but when I use
This is my code so far: class Compound(Item): def __init__(self, itemID, name, products, itemlist):
My question is pretty simple, I have: class upperstr(str): def __new__(cls, arg): return str.__new__(cls,
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private

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.