讓我們假設我有這個Dict:
d=Dict("arg1"=>10,"arg2"=>20)
和這樣的功能:
function foo(;arg1,arg2,arg3,arg4,arg5)
#Do something
end
如何呼叫函式并將Dictd 中的引數作為函式的引數傳遞?我知道我可以這樣做:
foo(arg1=d["arg1"],arg2=d["arg2"])
但是有什么方法可以自動化嗎?我的意思是一種找出在 Dict 中定義了哪些引數并將其自動傳遞給函式的方法。
uj5u.com熱心網友回復:
帶有Symbol鍵的字典可以直接splat輸入到函式呼叫中:
julia> d = Dict(:arg1 => 10, :arg2 => 12)
Dict{Symbol, Int64} with 2 entries:
:arg1 => 10
:arg2 => 12
julia> f(; arg1, arg2) = arg1 arg2
f (generic function with 1 method)
julia> f(; d...)
22
請注意函式呼叫中的分號,它確保將 ind中的元素解釋為關鍵字引數,而不是恰好是Pairs的位置引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/327748.html
下一篇:Azure函式沒有被執行
