# A good way of checking whether a keyword argument is present

**URL:** <https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199>\
**Category:** General Usage\
**Created:** [July 7, 2021, 8:35am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199 "2021-07-07T08:35:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [July 7, 2021, 8:35am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/1 "2021-07-07T08:35:30Z")

</div>

Hello,

I`d like to check for a presence of a particular keyword argument. I wanted to cross-check whether I am doing the right thing here.

This works but I feel using “filter” is perhaps an overkill and there might be a simpler way of achieving this.

```julia
function testKwargs(; kwargs...)
    # looking for keyword argument "width"
    width = isempty(filter(x->x.first==:width, kwargs)) ? 0 : kwargs[:width] 
end

testKwargs(; width=8, style="solid") # <--- gives 8
testKwargs(; widthh=8, style="solid") # <--- gives 0 (mind the misspelled "widthh")

```

---

<div class="post-metadata">

**Author:** ![guoyongzhi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guoyongzhi/32/28306_2.png) [@guoyongzhi](https://discourse.julialang.org/u/guoyongzhi)\
**Post date:** [July 7, 2021, 8:43am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/2 "2021-07-07T08:43:52Z")

</div>

```julia
function testKwargs(; kwargs...)
    # looking for keyword argument "width"
    get(kwargs, :width, 0)
end

```

Is that ok?

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [July 7, 2021, 8:46am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/3 "2021-07-07T08:46:41Z")

</div>

Absolutely! Thanks, this is neat!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 7, 2021, 8:54am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/4 "2021-07-07T08:54:07Z")

</div>

@guoyongzhi gave the right answer for the actual problem, but to answer the question in the title, you can check for presence of a keyword like this:

```julia
if :width in keys(kwargs)
    ...

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 7, 2021, 8:55am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/5 "2021-07-07T08:55:48Z")

</div>

Actually I wonder if you shouldn’t rather do this:

```julia
function testKwargs(; width=0, kwargs...)
    ...

```

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [July 7, 2021, 8:57am UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/6 "2021-07-07T08:57:23Z")

</div>

@sijo thanks for your additional thoughts. You are right about setting the default value in the function signature. Thanks!

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [July 7, 2021, 3:27pm UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/7 "2021-07-07T15:27:53Z")

</div>

Also note that signatures like

```julia
function testkwargs(; kwargs...)
    ...

```

will accept arbitrary keyword arguments. That may not be desired, since mistyping a keyword arg could silently fail, as you noted (i.e. the function would accept it, but fail to alert the user that the kwarg was incorrect, and the kwarg would not act as the user intended).

That behavior might be necessary for functions which might pass on kwargs to callbacks, etc., but the common way to define optional kwargs is to set the default value for the keyword arg to `nothing`, and then check for it in the function body:

```julia
function testkwargs(; width=nothing)
    if !isnothing(width)
        ...

```

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [July 9, 2021, 3:10pm UTC](https://discourse.julialang.org/t/a-good-way-of-checking-whether-a-keyword-argument-is-present/64199/8 "2021-07-09T15:10:24Z")

</div>

@halleysfifthinc thanks for sharing this perspective. Indeed, unless functions pass on kwargs to callbacks etc. it is a good practice to set each kwarg to nothing to enable dispatching mechanism to check on each associated keyword. The code quality goes up, I like that 🙂 .
