有时当我输入问号时,如下所示:
data["mass"]?
我收到语法错误。我必须使用 help(data["mass"])
才能获取信息。有什么想法可以让这项工作发挥作用吗?
最佳答案
我发现 ?
(和 ??
)在应用于变量或函数名称而不是表达式时效果最佳。
x = data['mass']
x?
y = 1.232
y?
z = 'astring'
z?
可能有一些表达式有效,但通常无效。我最常使用它来获取有关函数的信息,而不是获取任意对象的信息。在上面的表达式中,y?
为我提供了有关 float
(type(y)
创建者)的信息。
我怀疑?
使用了完成符
http://ipython.readthedocs.org/en/stable/api/generated/IPython.core.completer.html
The evaluation of the NAME.NAME... form may cause arbitrary application defined code to be executed if an object with a getattr hook is found. Since it is the responsibility of the application (or the user) to enable this feature, I consider this an acceptable risk. More complicated expressions (e.g. function calls or indexing operations) are not evaluated.
scipy.sparse
矩阵是定义自己的 __getattr__
的类的示例。
来自%magic
%pinfo:
Provide detailed information about an object.
'%pinfo object' is just a synonym for object? or ?object.
%pinfo2:
Provide extra detailed information about an object.
'%pinfo2 object' is just a synonym for object?? or ??object.
%pinfo
在 ?
返回语法错误
的情况下似乎返回未找到
。
In [265]: %pinfo data['mass']
Object `data['mass']` not found.
In [266]: %pinfo x+1
Object `x+1` not found.
In [267]: %pinfo data.get
Type: builtin_function_or_method
String form: <built-in method get of dict object at 0xb2d700ac>
Docstring: D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
关于ipython - 为什么问号在 iPython 中并不总是有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226945/