6

I would like to have autocompletion in IPython (Jupyter qtconsole or console) for the following case:

I create a class

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

and put several objects of this class into a list or dict

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

Now if I do

my_list[0].TAB

autocompletion is not working.

I would like to see a list of my class attributes and methods. Am I missing something or is this just not support in IPython?

Appreciate your help ...

jrjc
  • 21,103
  • 9
  • 64
  • 78
fred
  • 63
  • 1
  • 4

1 Answers1

11

You can execute that in a cell of your Jupyter Notebook:

%config IPCompleter.greedy=True

Which gives (in an ipython/jupyter console, but same in a notebook)

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

To have it permanently, just edit your file ipython_config.py so it looks like this (Commented lines are already present and unmodified, around lines 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

If you don't have ipython_config.py in ~/.ipython/profile_default/ you can create one with:

ipython profile create
jrjc
  • 21,103
  • 9
  • 64
  • 78
  • Thx a lot for this. Works perfect. The option is still in `ipython_config.py` which you most likely will find here `~/.ipython/profile_default/ipython_config.py`. If it is not present you can create it with `ipython profile create`. Since [The Big Split](http://blog.jupyter.org/2015/04/15/the-big-split/) IPython does not support profiles anymore. However, the default profile can still be altered. – fred Nov 03 '15 at 16:23
  • @fred, ah, you're right for the `ipython_profile`, I was just looking at another one from another profile, which why it wasn't working. Thanks, I'll edit accordingly. – jrjc Nov 03 '15 at 16:49