我来自MATLAB并习惯于whos获取形状和数据类型等变量信息,并经常使用特定的名称(例如whos Var1).
我知道我也可以whos在IPython中使用; 但是,当我有大量的变量和对象时,我希望能够一次检查一个并且MATLAB语法失败.
a = [1,2,3]
whos a
No variables match your requested type.
Run Code Online (Sandbox Code Playgroud)
我在Enthought Canopy IDE中使用了IPython shell.
有这个命令吗?
谢谢你,亚伦
mfi*_*tzp 18
命令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
Run Code Online (Sandbox Code Playgroud)
相关命令who或linemagic %who将生成一个短列表,仅显示变量名称:
who
a
who list
a
Run Code Online (Sandbox Code Playgroud)
要检查特定变量,?您需要的是:
a?
Type: list
String form: [1, 2, 3]
Length: 3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
Run Code Online (Sandbox Code Playgroud)
如果您想了解有关对象的更多信息,例如函数.您可以?在表单中使用两个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
Run Code Online (Sandbox Code Playgroud)