4

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

2
  • The way you've declared the exec method should accept multiple arguments. What error are you getting when you attempt to invoke it from IronPython? Commented Feb 27, 2010 at 15:21
  • The error is "Error-Code: 16; Message: syntax error". Commented Feb 28, 2010 at 10:12

2 Answers 2

3

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')(...)

Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

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] )

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.