How do I inspect one specific object in IPython

Question:

I’m coming from MATLAB and am used to the whos command to get variable information such as shape and data type and often used this with a specific names (e.g., whos Var1).

I know I can use whos in IPython as well; however, when I have a ton of variables and objects I’d like to be able to inspect one at a time and the MATLAB syntax fails.

a = [1,2,3]

whos a 
No variables match your requested type.

I’m using the IPython shell within the Enthought Canopy IDE.

Is there a command for this?

Thanks,
Aaron

Asked By: Aaron Rosenberg

||

Answers:

The command whos and linemagic %whos are available in IPython, but are not part of standard Python. Both of these will list current variables, along with some information about them. You can specify a type to filter by, e.g.

whos
Variable   Type    Data/Info
----------------------------
a          list    n=3
b          int     2
c          str     hello


whos list
Variable   Type    Data/Info
----------------------------
a          list    n=3

The related command who or linemagic %who will produce a short list, showing the variable names only:

who
a

who list
a    

To inspect a specific variable the ? is what you are looking for:

a?

Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

If you want even more information about an object, such as a function. You can use two ? in the form word?? to get the complete object help. For example, to get the complete documentation for the type int you would use:

int??
Type:        type
String form: <type 'int'>
Namespace:   Python builtin
Docstring:
int(x=0) -> int or long
int(x, base=10) -> int or long

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is floating point, the conversion truncates towards zero.
If x is outside the integer range, the function returns a long instead.

If x is not a number or if base is given, then x must be a string or
Unicode object representing an integer literal in the given base.  The
literal can be preceded by '+' or '-' and be surrounded by whitespace.
The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
Answered By: mfitzp

On google Colab, you can use https://github.com/google/etils

!pip install etils[ecolab]

Then

from etils import ecolab

ecolab.inspect(my_object)

This display an interactive visualisation to recursively inspect arbitrary python objects (similarly to Chrome JS console).

For example:

inspect screenshot

There’s also a ecolab.auto_inspect() which add an inspect button on all cells output:

screenshot inspect

I’m working on adding support for arbitrary jupyter notebook.

Answered By: Conchylicultor
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.