Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable sprint() to pass on kwargs #30708

Closed
wants to merge 1 commit into from

Conversation

mgkuhn
Copy link
Contributor

@mgkuhn mgkuhn commented Jan 13, 2019

A new optional keyword argument kwargs can now take a named tuple and Base.sprint will then pass on the content of that named tuple as keyword arguments to the function called.

A new optional keyword argument `kwargs` can now take a named tuple
and Base.sprint will then pass on the content of that tuple as
keyword arguments to the function called.
@StefanKarpinski
Copy link
Sponsor Member

Wouldn't it generally be clearer to pass a closure for this kind of usage?

@mgkuhn
Copy link
Contributor Author

mgkuhn commented Jan 13, 2019

Ah, thanks for the hint, I hadn't thought of that. Slightly slower, but not much:

using BenchmarkTools
wrap(io, strings...; open='(', close=')') = print(io, open, strings..., close)
a = 'a'
@btime sprint(wrap, a...; kwargs = (open='[',))
@btime sprint((s...) -> wrap(s...; open='['), a...)
@btime sprint((s...) -> wrap(s...), a...)
@btime sprint(wrap, a...)

gives

  683.224 ns (10 allocations: 448 bytes)
  1.035 μs (8 allocations: 368 bytes)
  662.226 ns (6 allocations: 320 bytes)
  598.449 ns (6 allocations: 320 bytes)

@kshyatt kshyatt added the domain:strings "Strings!" label Jan 13, 2019
@mgkuhn mgkuhn closed this Jan 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:strings "Strings!"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants