从 C# 调用具有多个 return 值的 Python 函数

Call a Python function with multiple return values from C#

This question 很好地概述了如何使用 IronPython 从 C# 调用 python 函数。

如果我们的 python 来源看起来像:

def get_x():
    return 1

基本方法是:

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

但是如果我们的 python 来源是:

def get_xyz():
    return 1, 2, 3

处理多个 return 值的 C# 语法是什么?

IronPython 运行时将 get_xyz() 的结果作为 PythonTuple 提供,这意味着它可以用作 IListICollectionIEnumerable<object> .. .

由于 C# 主要是静态性质,因此没有类似于 python 解包元组的方式的语法结构。通过提供的接口和集合 API,您可以接收值

var xyz = scope.get_xyz();
int x = xyz[0];
int y = xyz[1];
int z = xyz[2];