[IronPython] How do I run python functions and make the python code remember variables between calls from C#?
Curt Hagenlocher
curt at hagenlocher.org
Wed Mar 18 13:05:51 CET 2009
Your "global" statement needs to be inside your "set_crap" function.
On Wed, Mar 18, 2009 at 12:47 AM, BlackMan890 <blackman at simnet.is> wrote:
>
> Hi there.
>
> When I run the following .py from C#:
>
> --- test.py ----------------
>
> global crap
> crap = None
>
> def set_crap():
> crap = "yay"
>
> def return_crap():
> return crap
> ---------------------------
>
> And run the functions set_crap and return_crap from C# using the following
> code:
>
> --- program.cs ----------------
> --- Code is not being displayed right here, view the uploaded file I sent
> ---
> -------------------------------
>
> static void Main(string[] args)
> {
> ScriptEngine engine = Python.CreateEngine();
> ScriptSource source = engine.CreateScriptSourceFromFile("test.py");
> ScriptScope scope = engine.CreateScope();
>
> try
> {
>
> source.Execute(scope);
>
> }
> catch (Exception ex)
> {
> ExceptionOperations eo =
> engine.GetService<ExceptionOperations>();
> Console.WriteLine(eo.FormatException(ex));
> }
>
> SetData = scope.GetVariable<Func>("set_crap");
> GetData = scope.GetVariable<Func>("return_crap");
>
> SetData();
> object temp = GetData();
>
> Console.ReadLine();
> }
>
> static Func SetData;
> static Func GetData;
> -------------------------------
>
> The code compiles and runs but "temp" is always null.
> How would one go of running IronPython while maintaining all variables
> inside python?
> Cause the way I see it, whenever I run a function, all variables are
> resettet.
>
>
> Sincerely:
> Jonatan Nilsson
> Iceland
>
>
> P.S.
> I am using IronPython 2.0.1
>
> P.P.S.
> Your documentation for C# using IronPython 2.0.1 are nonexistant :S Where
> are the documentation on using IronPython 2.0.1 in C#?
> http://www.nabble.com/file/p22570584/program.cs program.cs
> --
> View this message in context:
> http://www.nabble.com/How-do-I-run-python-functions-and-make-the-python-code-remember-variables-between-calls-from-C---tp22570584p22570584.html
> Sent from the IronPython mailing list archive at Nabble.com.
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090318/a2d86233/attachment.html>
More information about the Ironpython-users
mailing list