Passing multiple arguments from IronPython.NET method.

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 use runtime in IronPython in my code to run python scripts, which in some cases should call the "exec" method. I would like to serve as a convenient way to call the exec method. Sort of:

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

But I do not know how to declare the "exec" method in C # to achieve this. In python, I can use the argument "* args":

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

I don’t want to have a separate Python method “exec” from the class “MyHelper”, because the class “MyHelper” provides complex functions in one place.

How do I write an "exec" method declaration in C # to achieve this? Or what other solution should I use?

thanks

+4
source share
2 answers

The problem is that "exec" is a keyword in Python, so you cannot use it as the name of your function. Instead, you can use "exec_" or execute or something similar. Alternatively you can write:

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

+3
source

According to this FAQ , MyHelper.exec , which you defined, should accept both array as the second argument, and any number of objects after the first string .

If your example helper call is not called as expected, this is probably a limitation of the IronPython interpreter and should probably be reported as an error . However, before reporting it as an error, create a minimally acceptable C # script that demonstrates what you are trying to do (show how it works in C #), and an IronPython script that tries to do the same but fails. This will be invaluable to solve the problem.

Meanwhile, why not just call

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

?

+1
source

Source: https://habr.com/ru/post/1302578/


All Articles