【问题标题】:Writing iron python method with ref or out parameter使用 ref 或 out 参数编写 Iron python 方法
【发布时间】: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


    【解决方案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

    【讨论】:

    • 我想尽可能简单地解释问题,这就是我使用该示例方法的原因。真正的问题是这个方法覆盖了基类中的方法,我必须在 IronPhyton 中提供相同的签名来覆盖基类方法。我试过你的方法,但我没有帮助......
    【解决方案2】:

    类似这样的 Python 脚本应该可以工作:

    ter = clr.Reference[System.String]()
    prov = clr.Reference[System.Int32]()
    
    GetTP('theName', ter, prov)
    
    print(ter.Value)
    print(prov.Value)
    

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2010-11-03
      相关资源
      最近更新 更多