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?

Community
  • 1
  • 1
Imran
  • 12,950
  • 8
  • 64
  • 79
  • 2
    Have you tried executing that and seeing what the return value is? – JLRishe Mar 05 '15 at 08: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. – Imran Mar 05 '15 at 14:55

1 Answers1

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];
Simon Opelt
  • 6,136
  • 2
  • 35
  • 66