[IronPython] Execute<T> and IronPython

Curt Hagenlocher curt at hagenlocher.org
Fri Oct 3 17:26:35 CEST 2008


I agree with you that hardcoding a specific variable name in your hosting
code feels like a poor design decision.  What I'd do instead is to import a
"set_return_value" function from the hosting app into the Python script,
then the last line of the script would be something like
"host.set_return_value(kitty)".  On some level, the two approaches are not
much different from each other -- but a hardcoded function name is an "API"
while a hardcoded variable name is "magic". :)
The notion of a return value from a script doesn't exist in the Python
language.

On Fri, Oct 3, 2008 at 12:12 AM, Leo Carbajal <desleo at gmail.com> wrote:

> Howdy again,
>
> According to the hosting spec each language defines how, and what, to
> return at the end of a script when using Execute<T>, is there a way to do
> this on IronPython? I've tried a couple of things, such as
>
> from Dirt import *
>
> def echo(x):
>     return x
>
> kitty = Cat()
> kitty.Name = "Tito"
> kitty.Meow()
> kitty
>
> As an alternative, I'm using the following with the above Python code.
>
>  public static T GetObject<T>(string fileName, string objectName)
>         {
>             try
>             {
>                 string path = String.Format("{0}{1}{2}.py", BaseDir,
> @"\HostScripts\", fileName);
>                 ScriptSource source =
> privateEngine.CreateScriptSourceFromFile(path);
>                 source.Execute();
>                 return
> privateEngine.GetVariable<T>(privateEngine.GetScope(path), objectName);
>             }
> //catches ommitted for space
> }
>
> Cat tito = DynamicServices.GetObject<Cat>("MUDConfig", "kitty");
> Console.WriteLine(tito.Name);
>
> I'd like to load my configuration files\classes and handle 'serialization'
> via Python scripts, for various reasons, and using Execute would prevent me
> from having to pick and choose specific names. (I.E. Kitty - since that
> would be hardcoded in the C# source)
>
> ---
> V
>
> _______________________________________________
> 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/20081003/e1b92256/attachment.html>


More information about the Ironpython-users mailing list