grokkaine
grokkaine

Reputation: 821

interactive use of python console inside function scope without debugging mode

Here is what I want:

def f():
    x = 3
    return

Running that in a console and asking for x will not give anything since x is inside the function scope. The alternative is to copy all code in there in a new script, untab it and run it. Then I can ask for the value of x. If I have a complex function f() or even worse, methods inside classes, then it can be a frustrating operation, to copy/untabify/interactively outputing and code modification/copy back/tabify properly.

The only way I can get interactive console inside a function scope is by using pdb, but I want a clean console in there and not a debuger.

> x
Traceback (most recent call last):

  File "<ipython-input-25-401b30e3b8b5>", line 1, in <module>
    x

NameError: name 'x' is not defined

Upvotes: 1

Views: 871

Answers (1)

Messa
Messa

Reputation: 25181

I use IPython as a little bit better Python console:

import IPython

def some_complex_fn():
    x = 42
    IPython.embed()

some_complex_fn()

The interactive console then knows about x.

I can't find whether the "classic" Python console has any similar API.

If you are writing a web application then a debugger with web interface might be better - for example the Werkzeug debugger includes an interactive console too.

Upvotes: 4

Related Questions