【问题标题】:Ipython autocompletion for list or dict of objects对象列表或字典的Ipython自动完成
【发布时间】:2015-11-03 11:59:44
【问题描述】:

对于以下情况,我希望在 IPython(Jupyter qtconsole 或控制台)中自动完成:

我创建了一个类

class MyClass(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b

并将这个类的几个对象放入一个列表或字典中

my_list = []
my_list.append(MyClass(2,3))
my_list.append(MyClass(9,2))
my_list.append(MyClass(8,4))

如果我这样做了

my_list[0].TAB

自动完成功能不起作用。

我想查看我的类属性和方法的列表。我是否遗漏了什么,或者这只是 IPython 不支持?

感谢您的帮助...

【问题讨论】:

    标签: ipython ipython-notebook jupyter qtconsole


    【解决方案1】:

    您可以在 Jupyter Notebook 的单元格中执行该操作:

    %config IPCompleter.greedy=True
    

    提供(在 ipython/jupyter 控制台中,但在笔记本中相同)

    In [10]: my_list[0].<TAB>
    my_list[0].a  my_list[0].b  
    

    要永久拥有它,只需编辑您的文件ipython_config.py,使其看起来像这样(注释行已经存在且未修改,大约在第 506-514 行):

    #------------------------------------------------------------------------------
    # Completer configuration
    #------------------------------------------------------------------------------
    
    # Activate greedy completion
    # 
    # This will enable completion on elements of lists, results of function calls,
    # etc., but can be unsafe because the code is actually evaluated on TAB.
    c.Completer.greedy = True # <-- uncomment this line and set it to True
    

    如果您在~/.ipython/profile_default/ 中没有ipython_config.py,您可以创建一个:

    ipython profile create
    

    【讨论】:

    • 非常感谢。完美运行。该选项仍在ipython_config.py 中,您很可能会在这里找到~/.ipython/profile_default/ipython_config.py。如果它不存在,您可以使用ipython profile create 创建它。由于The Big Split IPython 不再支持配置文件。但是,仍然可以更改默认配置文件。
    • @fred,啊,你是正确的ipython_profile,我只是从另一个配置文件中查看另一个,这为什么它不起作用。谢谢,我会相应地编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2014-02-23
    • 2013-08-01
    • 2012-09-17
    相关资源
    最近更新 更多