Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inspect one specific object in IPython

Tags:

python

ipython

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

like image 796
Aaron Rosenberg Avatar asked Feb 23 '15 16:02

Aaron Rosenberg


People also ask

How do you identify an object in IPython?

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.

How do you inspect an object in a Jupyter notebook?

The JupyterLab inspector You can open the inspector using the Ctrl + I shortcut (or ⌘ + I on Mac). With the Python kernel (ipykernel or xeus-python), this contextual help normally contains a text representation generated using the inspect Python module and the docstrings associated to an object.

How do you display data from an object in Python?

Use Python's vars() to Print an Object's Attributes The dir() function, as shown above, prints all of the attributes of a Python object. Let's say you only wanted to print the object's instance attributes as well as their values, we can use the vars() function.


1 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
like image 182
mfitzp Avatar answered Sep 18 '22 17:09

mfitzp