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

Related Questions

Is there anything in Python that works like the final keyword in Java -
Is there anything that performs the following, in python? Or will I have to
Is there anything for Python that has concurrency like Erlang does, particulary transparent actors
Is there anything in Python akin to Java's JLS or C#'s spec?
Is there anything equivalent or close in terms of functionality to Python's virtualenv ,
Is there anything wrong or inherently unsafe about the way I've programmed this? I'm
Is there anything similar to the Spring JMX exporter out there WITHOUT using the
I'm working on a Python class that lets the caller add widgets to a
Is there anything that should be done to make GNU Emacs 23.2 work well
Is there anything similar to the following in Windows that will let me know

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.