views:

157

answers:

2

Hi,

I have a class in .NET (C#):

public class MyHelper {
    public object exec( string script, params object[] arguments ) {
        // execute script with passed arguments in some external enviroment
    }
}

I'm using IronPython runtime in my code to run python scripts, which should in some cases call the "exec" method. I would like serve the comfortable way to call the "exec" method. Something like:

helper.exec( "someExternalFunction( {0}, {1}, {3} )", var01, var02, var03 )

But I don't know how to declare the "exec" method in C# to achieve this. In python I can use a "*args" argument:

def exec( script, *args ):
    ... do something ...

I don't want have separate Python method "exec" from "MyHelper" class, because the "MyHelper" class provides complex functionality "in one place".

How should I write the "exec" method declaration in C# to achieve that? Or what other solution should I use?

Thanks

+1  A: 

According to this FAQ, the MyHelper.exec you've defined should accept both an array as the second argument, or any number of objects following the first string.

If your example call to helper does not invoke as expected, that is probably a limitation of the IronPython interpreter, and probably needs to be filed as a bug. Before filing it as a bug, however, please create a minimal runnable C# script that demonstrates what you're trying to do (show how it works as expected in C#), and an IronPython script that attempts to do the same thing but fails. This will be invaluable toward making the problem work.

In the meantime, why not just call

helper.exec( "someExternalFunction( {0}, {1}, {3} )", [var01, var02, var03] )

?

Jason R. Coombs
+3  A: 

The problem here is that "exec" is a keyword in Python so you can't use that as your function name. You could use "exec_" or execute or something like that instead. Alternately you could write:

getattr(helper, 'exec')(...)

Dino Viehland
You are right. There was primairly problem with the name "exec".I'm beginner in Python and I didn't know the "exec" is keyword (my fault).Thanks!
TcKs