如何打印我定义的对象的源代码使用%edit magic [英] how to print source code of object I defined use %edit magic

查看:105
本文介绍了如何打印我定义的对象的源代码使用%edit magic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ipython 0.13.1可以从python库打印一个对象的源代码,

如os.path.abspath ??

但我无法打印源代码我在ipython中通过%ed magic定义的任何对象的代码,

我做错了什么?

比如,我通过%ed magic定义了一个类名:

Ipython 0.13.1 can print the source of an object from python library,
such as, os.path.abspath??
But I can't print the source code of any object I defined through %ed magic in ipython,
Is anything wrong I did?
such as, I define a class Name through %ed magic:

%ed  

那么

class Name(object):
    """docstring for Name"""
    name = 'hong'
    def __init__(self, arg):
        super(Name, self).__init__()
        self.arg = arg
    def pri():
        print 'class Name'

回到ipython时,我看不到源代码类名:

when back to ipython, I can't see the source code of class Name:

In [59]: Name??
Type:       type
String Form:<class '__main__.Name'>
Docstring:  docstring for Name
Constructor information:
Definition:Name(self, arg)

这是IPython的错误吗?

Is this the bug of IPython?

推荐答案

- OP之后的编辑提到这是在ipython中看到的我自己。

--Edits after OP mentioned this is seen in ipython iteself.

在排除%ed myfunc之后是否有任何错误消息?粘贴可能有助于其他人找到问题。

Is there any error message you get after typying %ed myfunc? Pasting that might help others find the issue.

---更新:

我也得到一个简短的版本我尝试Name时的源代码,但Name.pri ??给我一个Name类的pri()成员函数的完整源代码。因此ipython可能有一些惯例不能提供类的完整源代码。

I also get a short version of the source code when I try Name??, but Name.pri?? gives me the full source code of the pri() member function of the Name class. So ipython may have some convention to not give full source code of classes.

这是我的互动:

    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]:

这篇关于如何打印我定义的对象的源代码使用%edit magic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆