【发布时间】:2010-05-18 12:41:12
【问题描述】:
我需要将以下 C# 方法转换为相同的 IronPhyton 方法
private void GetTP(string name, out string ter, out int prov)
{
ter = 2;
prov = 1;
}
【问题讨论】:
标签: c# ironpython
我需要将以下 C# 方法转换为相同的 IronPhyton 方法
private void GetTP(string name, out string ter, out int prov)
{
ter = 2;
prov = 1;
}
【问题讨论】:
标签: c# ironpython
在 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)
【讨论】: