[Ironpython-users] scope get/set variable and invoking python function

Bill bg271828 at gmail.com
Thu Apr 5 06:38:41 CEST 2012


I am having a problem in ironpython 2.7.2.1 with executing a python
function that changes variables set in the scope via SetVariable. The
problem is these variables are never actually updated on the c# side
after the python function executes. Here is a couple different
examples, all of which don't work:

The python code:

def TestFunction():
    Test1 = 4

The c# code:

Example 1:

ScriptEngine sEng = Python.CreateEngine();
ScriptScope scope;
dynamic fHand;

scope = sEng.Runtime.CreateScope();

scope.SetVariable("Test1", 0);

fHand = sEng.ExecuteFile("c:\test.py"); // Contains python code listed above
if (scope.ContainsVariable("TestFunction"))
    fHand.TestFunction();

int result = scope.GetVariable<int>("Test1"); // This is still set to
0 when it should be set to 4 now.

Example 2:

ScriptScope scope = Python.CreateRuntime().UseFile("c:\test.py");
dynamic fHand = scope;
scope.SetVariable("Test1", 0);

if (scope.ContainsVariable("TestFunction"))
    fHand.TestFunction();

int result = scope.GetVariable<int>("Test1"); // This is still set to
0 when it should be set to 4 now.


What am I missing here? Is it really that python functions cannot
update variables set in the scope? Python code outside of functions
are able to update them without any issues.


More information about the Ironpython-users mailing list