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

标签 python 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 的一部分。这两个都将列出当前变量,以及有关它们的一些信息。您可以指定一个类型作为过滤依据,例如

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/

相关文章:

python - __repr__ 装饰器在子类上失败

python - 带有隐形字符的新行

python - 随后运行多个 tensorflow session

python - 为什么 IPython.display.Image 没有显示在输出中?

python - IPython sys.path 不同于 python sys.path

python - Keras - 如何根据一个实例进行预测?

python - 求和最大路径算法给出了意想不到的解决方案

python - 当 Python 从命令行运行时导入 urllib.parse 失败

Vim 编辑器 - zsh shell ipython magic %ed 找不到编辑器

python - 如何用Python制作一个像C++一样的计算器