InteractiveConsole and namespace

D

David ROBERT

Hi all,

I want to use an InteractiveConsole at some stage in a program to
interact with the local namespace: access, but also modify objects.
When the interactive console ends (ctrl-d) I want the program to
continue processing with the variables that may have been modified
interactively.

The code below works (block invoking the console is not in a
function). During the interactive session, I can read value of a, I
can change value of a and the new value is "updated" in the block
namespace.

import code
if __name__ == '__main__':
a=1
c = code.InteractiveConsole(locals())
c.interact() # Here I interactively change the value of a (a=2)
print "Value of a: ", a

print returns --> Value of a: 2

However, on the other code below (the console is invoked from within a
function block), during the interactive session, I can read value of
a, I can change value of a. But the local namespace of the function is
not updated:

import code
def test():
a=1
c = code.InteractiveConsole(locals())
c.interact() # Here I interactively change the value of a (a=2)
print "Value of a: ", a

if __name__ == '__main__':
test()

print returns --> Value of a: 1

I need to run the InteractiveConsole from a function block. I tried
different things with the local and parent frames (sys._getframe())
but nothing successful. If I put a in the global namespace it works,
but I would like to find a nicer solution and also understand what the
problem is.

Thanks for any help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top