如何检查 IPython 中的一个特定对象

How do I inspect one specific object in IPython

我来自 MATLAB,习惯使用 whos 命令来获取形状和数据类型等变量信息,并且经常将其与特定名称一起使用(例如,whos Var1)。

我知道我也可以在 IPython 中使用 whos;然而,当我有大量变量和对象时,我希望能够一次检查一个,而 MATLAB 语法失败了。

a = [1,2,3]

whos a 
No variables match your requested type.

我在 Enthought Canopy IDE 中使用 IPython shell。

有这方面的命令吗?

谢谢, 亚伦

命令 whos 和 linemagic %whos 在 IPython 中可用,但不是标准 Python 的一部分。这两个都将列出当前变量,以及有关它们的一些信息。您可以指定 type 作为过滤依据,例如

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

相关命令who或linemagic %who会生成一个简短的列表,只显示变量名:

who
a

who list
a    

要检查特定变量,? 就是您要查找的内容:

a?

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

如果您想要更多有关对象(例如函数)的信息。您可以使用 word?? 形式的两个 ? 来获得完整的对象帮助。例如,要获取类型 int 的完整文档,您可以使用:

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