如何在Julia的dict中获取关键字参数及其函数的默认值?
E.g:
function foo(x; a = 1, b = 2, c= 3)
# How do I get a dict of keyword arguments: Dict(a=>1, b=>2,c=3) ??,
# so I can pass this Dict easily to another generic function taking v
# variable keyword arguments for further processing
end
答案 0 :(得分:4)
您可以使用splat ...
运算符:
function foo(x; kwargs...)
Dict(kwargs)
end
或者如果您只想传递它:
function foo(x; kwargs...)
innerfunction(x; kwargs...)
end
答案 1 :(得分:1)
只需像这样创建一个Dict:
function foo(x; a = 1, b = 2, c= 3)
Dict(:a => a, :b => b, :c => c)
end