TPython::Eval for locals

Is there a way to use TPython::Eval for a variable with local scope or is it limited to globals? Details follow.


For example, if I declare a variable inside a function, TPython does not know what it is:

In [1]: import ROOT

In [2]: def afunc():
   ...:     valdict = {}
   ...:     ROOT.gInterpreter.ProcessLine('TPython::Eval("valdict")')
   ...:     

In [3]: afunc()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<string> in <module>()

NameError: name 'valdict' is not defined
(const TPyReturn) @0x7fd956c0a780

If, however, I make that variable global, TPython can find it just fine:

In [5]: def afunc():
   ...:     global valdict
   ...:     valdict = {}
   ...:     ROOT.gInterpreter.ProcessLine('TPython::Eval("valdict")')
   ...:     

In [6]: afunc()
(const TPyReturn) @0x7fd956bbc3d0

ROOT Version: 6.19/01
Platform: macOS Mojave
Compiler: Not Provided


I think @etejedor can most probably help you

Hi,
When you run TPython::Eval, it will evaluate the expression you provide in the Python global scope. The fact that you invoke it from inside a Python function does not change that.

I meant to ask if there was an additional option I could set to let me evaluate locals. It’s implicit in your answer that I cannot. Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.