How to optionally pass arguments with non-default values in Python?
<p>I have a following method:</p>

<pre><code>def a(b='default_b', c='default_c', d='default_d'):
    # â¦
    pass
</code></pre>

<p>And I have some command-line interface that allows either to provide variables <code>user_b</code>, <code>user_c</code>, <code>user_d</code> or to set them to <code>None</code> - i.e. the simple Python <code>argparse</code> module.</p>

<p>I want to make a call like:</p>

<pre><code>a(b=user_b if user_b is not None,
  c=user_c if user_c is not None,
  d=user_d if user_d is not None)
</code></pre>

<p>If the variable is <code>None</code> I want to use a default value from the method's argument.</p>

<p>The only way I found is to check all the combinations of the user variables:</p>

<pre><code>if not user_b and not user_c and not user_d:
    a()
elif not user_b and not user_c and user_d:
    a(d)
â¦
elif user_b and user_c and user_d:
    a(b, c, d)
</code></pre>

<p>What is more efficient, fancy and Pythonic way to solve my problem?</p>

<p>You could write a function that filters out unwanted values then use it to scrub the target function call.</p>

<pre><code>def scrub_params(**kw):
    return {k:v for k,v in kw if v is not None}

some_function(**scrub_params(foo=args.foo, bar=args.bar))
</code></pre>

python syntactic-sugar