qed qed - 26 days ago 5
Python Question

How to get the total number of inputs in a ipython session?

For example in a session like the following:

In [1]: x = 1

In [2]: y = 2

In [3]: z = 3


How can I programmatically find out that there are so far 3 inputs?

Answer

You could use the _ih ipython internal variable and call it using globals():

In [1]: x = 1

In [2]: y = 2

In [3]: z = 3

In [4]: len(globals()['_ih'])
Out[4]: 5

In [5]: print(globals()['_ih'])
['', 'x = 1', 'y = 2', 'z = 3', "len(globals()['_ih'])", "print(globals()['_ih'])"]