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:

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).
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.
Original answer by titusjan
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With