4

In Chrome development console one can type name of a variable and output is a visual and interactive representation of the object. (In JavaScript objects are like dictionaries, so this is easy.)

I would like to have the same functionality in Python. I am shocked I cannot find anything similar. For example in IPython GUI console when I want to inspect variable diff, I get in this case its type instead:

In [5]: diff
Out[5]: <_pygit2.Diff at 0x1a69930>

This command inspect the variable, but output is chaotic for complicated objects (here the output is incomplete):

In [10]: inspect.getmembers(diff)
Out[10]: [('__class__', <type '_pygit2.Diff'>), ('__delattr__', <method-wrapper '__delattr__' of _pygit2.Diff object at 0x1a69930>), ('__doc__', 'Diff objects.'), ('__format__', <built-in method __format__ of _pygit2.Diff object at 0x1a69930>), ('__getattribute__', (...)

I think a live introspection is very useful when language has no typesystem. Maybe this functionality is available only in special Python IDEs?

To show how it is done in Chrome:

example of Chrome devtools object introspection

On the picture you can see an introspecion of variable f. It is an object of type Form, you can click on it and see its properties (e.g. _meetingTimeFrom) and theirs values, you can click further on properties to inspect them, you can see object's methods (e.g. field __proto__, this might be a way how to see object methods in JavaScript).

Mykola
  • 3,343
  • 6
  • 23
  • 39

1 Answers1

2

If you just want to list attributes in readable way, pprint.pprint is your friend:

from pprint import pprint
from inspect import getmembers

class X:
    def __init__(self, x, y):
        self.x = x
        self.y = y

x = X([42] * 5, [True] * 15)

pprint(getmembers(x))
pprint(vars(x))

If you're interested in a GUI for this, take a look at objbrowser. It uses the inspect module from the Python standard library for the object introspection underneath.

objbrowserscreenshot

Original answer by titusjan

Community
  • 1
  • 1
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
  • 1
    Although this answer is sufficient for me, thank you for it, I would like to post again a link that is in comments bellow my question where are other interesting answers: http://stackoverflow.com/questions/15487839/is-it-possible-to-display-an-objects-instance-variables-in-ipython-like-matlab-d – Martin Jiřička Feb 02 '16 at 08:53