我来自 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 的一部分。这两个都将列出当前变量,以及有关它们的一些信息。您可以指定一个类型
作为过滤依据,例如
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
关于python - 如何检查 IPython 中的一个特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28678438/