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
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.
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.
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.
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
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