我需要将以下 C# 方法转换为相同的 IronPhyton 方法
private void GetTP(string name, out string ter, out int prov)
{
ter = 2;
prov = 1;
}
最佳答案
在 python 中(因此在 IronPython 中)你不能更改不可变的参数(如字符串)
所以你不能直接将给定的代码翻译成 python,但你必须这样做:
def GetTP(name):
return tuple([2, 1])
当你调用它时你必须做:
retTuple = GetTP(name)
ter = retTuple[0]
prov = retTuple[1]
这与在 IronPython 中调用包含 out/ref 参数的 C# 方法时的行为相同。
事实上,在那种情况下,IronPython 返回一个 out/ref 参数的元组,如果有返回值,则返回元组中的第一个值。
编辑: 实际上可以用 out/ref 参数覆盖一个方法,看这里:
http://ironpython.net/documentation/dotnet/dotnet.html#methods-with-ref-or-out-parameters
关于c# - 使用 ref 或 out 参数编写 iron python 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2857287/