我需要将以下C#方法翻译成相同的IronPython方法:
private void GetTP(string name, out string ter, out int prov)
{
ter = 2;
prov = 1;
}
我需要将以下C#方法翻译成相同的IronPython方法:
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
像这样的Python脚本应该可以工作:
ter = clr.Reference[System.String]()
prov = clr.Reference[System.Int32]()
GetTP('theName', ter, prov)
print(ter.Value)
print(prov.Value)