用ref或out参数编写Iron Python方法 [英] Writing iron python method with ref or out parameter
问题描述
我需要将以下C#方法转换为相同的IronPhyton方法
i need to translate following C# method to the same IronPhyton method
private void GetTP(string name, out string ter, out int prov)
{
ter = 2;
prov = 1;
}
推荐答案
在python中(因此在IronPython中),您不能更改不可变的参数(如字符串)
In python (and consequently in IronPython) you cannot change a parameter that is not mutable (like strings)
因此您不能直接将给定的代码翻译为python,但必须执行以下操作:
So you can't directly traslate the given code to python, but you must do something like:
def GetTP(name):
return tuple([2, 1])
当您调用它时,您必须执行以下操作:
and when you call it you must do:
retTuple = GetTP(name)
ter = retTuple[0]
prov = retTuple[1]
在IronPython中调用包含out/ref参数的C#方法时,这是相同的行为.
that is the same behaviour when in IronPython you call a C# method containing out/ref parameters.
实际上,在这种情况下,IronPython返回一个out/ref参数元组,如果有返回值,则该元组中的第一个.
In fact, in that case IronPython returns a tuple of out/ref parameters, and if there's a return value is the first in the tuple.
实际上,可以使用out/ref参数覆盖方法,请看这里:
actually it's possible to override a method with out/ref parameters, look here:
http://ironpython.net/documentation/dotnet/dotnet.html#methods-with-ref-or-out-parameters
这篇关于用ref或out参数编写Iron Python方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!