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

  • Home
  • SEARCH
  • 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 9188719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:04:54+00:00 2026-06-17T20:04:54+00:00

I am reading DiveIntoPython. I’m wondering why I can directly print the instance of

  • 0

I am reading DiveIntoPython.

I’m wondering why I can directly print the instance of a UserDict as a dictionary.
In detail, these codes

import UserDict;

d = UserDict.UserDict({1:1, 'a':2});
print d;
print d.data;

will have output

{'a': 2, 1: 1}
{'a': 2, 1: 1}

And these codes

class MyDict:
    def __init__(self, dictData=None):
        self.data = dictData;

d = MyDict({1:1, 'a':2});
print d;
print d.data;

will have output (on my machine)

<__main__.MyDict instance at 0x10049ef80>
{'a': 2, 1: 1}

In other words, How I can define my class, and print its instances as a built-in datatype?
Thank you!

  • 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-17T20:04:55+00:00Added an answer on June 17, 2026 at 8:04 pm

    How an object is printed comes down to it’s repr – when you inherit from a mixin, it already provides the repr function. Also note, these days you can just inherit from dict directly.

    In your case, you can define

    def __repr__(self):
        return repr(self.data)
    

    The difference between __str__ and __repr__ is that mostly __str__ should be readable and understable. __repr__ where it’s possible can be used to provide an eval‘d string constructing the original object (although not necessary) – see the great answer here for the difference: Difference between __str__ and __repr__ in Python

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

Sidebar

Related Questions

reading the documentation for java org.w3c.dom.ls it seems as a Element only can be
Reading the bottom of the documentation, specifically: You can still retrieve properties of deleted
Reading some topics I found this piece of code, and I'm wondering, how does
Reading the documentation I can see that + operator can be used to compose/combine
reading diveintopython3 came upon this and can't deconstruction into smaller bits i understand. >>>
Reading Real world Haskell i found some intresting question about data types: This pattern
Reading from the googleVis package vignette : With the googleVis package users can create
//Reading data from property file : String username = prop.getProperty(username); String password = prop.getProperty(password);
Reading this excellent answer about password hashing and wondering how to implement it: The
Reading Monitoring certain system calls done by a process in Windows , I'm wondering

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.