簡體   English   中英

用ref或out參數編寫鐵python方法

[英]Writing iron python method with ref or out parameter

我需要將以下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

像這樣的Python腳本應該工作:

ter = clr.Reference[System.String]()
prov = clr.Reference[System.Int32]()

GetTP('theName', ter, prov)

print(ter.Value)
print(prov.Value)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM