Ipython 0.13.1可以从python库打印一个对象的源,
比如,os.path.abspath ??
但我不能打印我在ipython中通过%ed magic定义的任何对象的源代码,我
做错了什么?
比如,我通过%ed magic定义一个类名:
%ed
Run Code Online (Sandbox Code Playgroud)
然后
class Name(object):
"""docstring for Name"""
name = 'hong'
def __init__(self, arg):
super(Name, self).__init__()
self.arg = arg
def pri():
print 'class Name'
Run Code Online (Sandbox Code Playgroud)
回到ipython时,我看不到类Name的源代码:
In [59]: Name??
Type: type
String Form:<class '__main__.Name'>
Docstring: docstring for Name
Constructor information:
Definition:Name(self, arg)
Run Code Online (Sandbox Code Playgroud)
这是IPython的错误吗?
nom*_*-ir 10
- 在OP提到之后的编辑可以在ipython iteself中看到.
排除%ed myfunc后是否有任何错误消息?粘贴可能有助于其他人找到问题.
---更新:
当我尝试Name ??时,我也得到了源代码的简短版本,但Name.pri ?? 给我一个Name类的pri()成员函数的完整源代码.所以ipython可能有一些约定,不能提供类的完整源代码.
这是我的互动:
In [2]: Name??
Type: type
String Form:<class '__main__.Name'>
Docstring: docstring for Name
Constructor information:
Definition:Name(self, arg)
In [3]: Name.pri??
Type: instancemethod
String Form:<unbound method Name.pri>
File: /tmp/ipython_edit_8YOfN9.py
Definition: Name.pri()
Source:
def pri():
print 'class Name'
In [4]:
Run Code Online (Sandbox Code Playgroud)
IPython 的最新版本(不确定确切的版本号)实际上确实显示了源代码:
\n\nIPython 0.13 -- An enhanced Interactive Python.\n? -> Introduction and overview of IPython's features.\n%quickref -> Quick reference.\nhelp -> Python's own help system.\nobject? -> Details about 'object', use 'object??' for extra details.\n\nIn [1]: %ed\nIPython will make a temporary file named: /var/folders/88/zgy_z51x1fn2mp_7vmkj3phm0000gn/T/ipython_edit_g9xYY4.py\nEditing... done. Executing edited code...\nOut[1]: 'def f(a):\\n\\treturn a + 3\\n'\n\nIn [2]: f(4)\nOut[2]: 7\n\nIn [3]: f??\nType: function\nString Form:<function f at 0x18ddb30>\nFile: /var/folders/88/zgy_z51x1fn2mp_7vmkj3phm0000gn/T/ipython_edit_g9xYY4.py\nDefinition: f(a)\nSource:\ndef f(a):\n return a + 3\nRun Code Online (Sandbox Code Playgroud)\n\n还有另外两种获取源代码的方法。一是如果你可以返回到你调用的那一行%ed,返回值应该是源代码(这里,这是Out[1],所以你可以这样做print Out[1])。另一种方法是调用%ed f\xe2\x80\x94 ,这应该将当前定义加载f到编辑器中,并允许您编辑定义。
| 归档时间: |
|
| 查看次数: |
10409 次 |
| 最近记录: |