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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:53:00+00:00 2026-05-15T16:53:00+00:00

Is there anything in python that lets me dump out a random object in

  • 0

Is there anything in python that lets me dump out a random object in such a way as to see its underlying data representation?

I am coming from Perl where Data::Dumper does a reasonable job of letting me see how a data structure is laid out. Is there anything that does the same thing in python?

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-05-15T16:53:01+00:00Added an answer on May 15, 2026 at 4:53 pm

    Well Dumper in Perl gives you a representation of an object that can be eval‘d by the interpreter to give you the original object. An object’s repr in Python tries to do that, and sometimes it’s possible. A dict‘s repr or a str‘s repr do this, and some classes like datetime and timedelta also do this. So repr is as much the equivalent of Dumper but it’s not pretty and doesn’t show you the internals of an object. For that you can use dir and roll your own printer.

    Here’s my shot at a printer that would not result in eval-able Python code and thus should be used to generate a string of the object instead:

    def dump(obj):
      out = {}
    
      for attr in dir(obj):
        out[attr] = getattr(obj, attr)
    
      from pprint import pformat
      return pformat(out)
    
    class myclass(object):
      foo = 'foo'
    
      def __init__(self):
        self.bar = 'bar'
    
      def __str__(self):
        return dump(self)
    
    c = myclass()
    print c
    

    In the above example, I’ve overridden the object’s default __str__ implementation. __str__ is what gets called when you try to represent the object as a string, or format it using a string formatting function.

    BTW repr is what gets printed when you do print obj, it invokes the __repr__ method on that object. See the Python documentation of __repr__ for more information on how to control the formatting of objects.

    # this would print the object's __repr__
    print "%r" % c
    
    # this would print the object's __str__
    print "%s" % c
    

    The output from the above code was

    {'__class__': <class '__main__.myclass'>,
     '__delattr__': <method-wrapper '__delattr__' of myclass object at 0xb76deb0c>,
     '__dict__': {'bar': 'bar'},
     '__doc__': None,
     '__format__': <built-in method __format__ of myclass object at 0xb76deb0c>,
     '__getattribute__': <method-wrapper '__getattribute__' of myclass object at 0xb76deb0c>,
     '__hash__': <method-wrapper '__hash__' of myclass object at 0xb76deb0c>,
     '__init__': <bound method myclass.__init__ of <__main__.myclass object at 0xb76deb0c>>,
     '__module__': '__main__',
     '__new__': <built-in method __new__ of type object at 0x82358a0>,
     '__reduce__': <built-in method __reduce__ of myclass object at 0xb76deb0c>,
     '__reduce_ex__': <built-in method __reduce_ex__ of myclass object at 0xb76deb0c>,
     '__repr__': <method-wrapper '__repr__' of myclass object at 0xb76deb0c>,
     '__setattr__': <method-wrapper '__setattr__' of myclass object at 0xb76deb0c>,
     '__sizeof__': <built-in method __sizeof__ of myclass object at 0xb76deb0c>,
     '__str__': <bound method myclass.__str__ of <__main__.myclass object at 0xb76deb0c>>,
     '__subclasshook__': <built-in method __subclasshook__ of type object at 0x896ad34>,
     '__weakref__': None,
     'bar': 'bar',
     'foo': 'foo'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer basically, you want to strip off the season? do a… May 17, 2026 at 2:05 am
  • Editorial Team
    Editorial Team added an answer Have you tried wrapping the syndication view django.contrib.syndication.views.feed into a… May 17, 2026 at 2:05 am
  • Editorial Team
    Editorial Team added an answer If you can't use negative ids simply add a number… May 17, 2026 at 2:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

There's a nice plugin for Frog CMS that lets you just type in yourpicture.120x120.jpg
I have a python webapp which accepts some data via POST. The method which
Before anything, let me first clarify that the below thoughts are purely my personal
I'd like to change the behavior of Python's list displays so that instead of
I'm trying to work with some long file paths (Windows) in Python and have
I have a remote database (at the moment sqlite, but eventually mysql) that I
I'm currently implementing the Factory design pattern in Python and I have a few
I'm just trying to hook Trac/SVN together so that my SVN commits manage my
Let's say you have a small calculator program that takes numbers and an operator
In the last month, we've had a persistent problem with the Python 2.6.x multiprocessing

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.