[IronPython] Newbie: convert string to python expression??
Michael Foord
fuzzyman at voidspace.org.uk
Tue Dec 16 13:32:24 CET 2008
xkrja wrote:
> Thanks for the replies guys.
>
> I tried the solution below and it works in some cases. But a strange thing
> is (remember I'm a newbie) that if I first type, for example a=1 in the
> "inputBox" and then hit enter, the debugger goes down to exec
> root.inputBox.Text and nothing is reported so I assume the statement is
> executed correctly.
>
> But if I next time type print a in the "inputBox" the debugger goes down to
> exec root.inputBox.Text again but now an exception is raised: "name 'a' is
> not defined". Why is that??? I typed a=1!
>
You've snipped the code so I can't see it exactly, but I'm pretty sure
you are doing this inside a method. This will create a new 'scope' every
time you enter the method and so you are creating a local variable that
disappears when you exit the method.
When you exec you can provide a dictionary as a context for the
execution to happen in. If you store this as an instance member and
re-use the same execution context every time then changes will be
'remembered'.
Michael
> Thanks!
>
>
> Douglas Blank wrote:
>
>> This is just a Python issue (not specific to IronPython).
>>
>> exec is used for statement(s) and eval() for expressions. You might have
>> to:
>>
>> try:
>> eval(root.inputBox.Text)
>> except:
>> exec root.inputBox.Text
>>
>> -Doug
>>
>>
>>
>>
>>
>
>
--
http://www.ironpythoninaction.com/
More information about the Ironpython-users
mailing list