2

This question gives a nice overview of how to call a python function from C# using IronPython.

If our python source looks like:

def get_x():
    return 1

The basic approach is:

var script = @"
def get_x():
    return 1";

var engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();
engine.Execute(script, scope);

var x = scope.get_x();
Console.WriteLine("x is {0}", x);

But what if our python source is:

def get_xyz():
    return 1, 2, 3

What is the C# syntax for handling multiple return values?

2
  • 2
    Have you tried executing that and seeing what the return value is? Commented Mar 5, 2015 at 8:57
  • 1
    No, I don't have a C# environment set up yet. I am working on the Python side and trying to help someone connect to it from C#, so I'm a bit limited right now. Commented Mar 5, 2015 at 14:55

1 Answer 1

3

The IronPython runtime provides the result of get_xyz() as a PythonTuple which means it can be used as IList, ICollection, IEnumerable<object> ...

Because of the primarily static nature of C# there are no syntax constructs similar to python's way of unpacking tuples. Through the provided interfaces and collection APIs you could receive the values as

var xyz = scope.get_xyz();
int x = xyz[0];
int y = xyz[1];
int z = xyz[2];
Sign up to request clarification or add additional context in comments.

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.