I wonder if there is a way to pass on keyword arguments through a thin wrapper without having to explicitly type the keyword arguments, something like the following Python code. Splatting doesn’t seem to work, nor does passing a dictionary, anything other obvious way I missed? Will this change with named tuples in v1.0?
>>> def f(**kwargs):
... return kwargs
...
>>> f(a = 1, b = 2, c = 3)
{'a': 1, 'c': 3, 'b': 2}
>>> type(f(a = 1, b = 2, c = 3))
<type 'dict'>
>>> def g(**kwargs):
... return f(**kwargs)
...
>>> g(a = 1, b = 2, c = 3)
{'a': 1, 'c': 3, 'b': 2}
This is what I missed
julia> f(;kwargs...) = Dict(kwargs)
f (generic function with 1 method)
julia> g(;kwargs...) = f(;kwargs...)
g (generic function with 1 method)
julia> g(a = 1, b = 2, c = 3)
Dict{Symbol,Int64} with 3 entries:
:a => 1
:b => 2
:c => 3
Thanks to @mkborregaard in How to collect keyword arguments in a Dict? - #5 by mkborregaard
2 Likes
Why do you have to recast the kwargs
as a dictionary though?
It seems kind of weird you need an intermediate function that is just calling a Dict
constructor?
The way I work around it is with:
default_kwargs = Dict()
f(kwargs=default_kwargs)
I don’t remember! It was me answering another question or some exercise or something.
Isn’t that just to show that the kwargs got passed through.
1 Like