title: Call function without optional arguments if they are None
body: <p>There's a function which takes optional arguments.</p>
  
  <pre><code>def alpha(p1="foo", p2="bar"):
       print('{0},{1}'.format(p1, p2))
  </code></pre>
  
  <p>Let me iterate over what happens when we use that function in different ways:</p>
  
  <pre><code>&gt;&gt;&gt; alpha()
  foo,bar
  &gt;&gt;&gt; alpha("FOO")
  FOO,bar
  &gt;&gt;&gt; alpha(p2="BAR")
  foo,BAR
  &gt;&gt;&gt; alpha(p1="FOO", p2=None)
  FOO,None
  </code></pre>
  
  <p>Now consider the case where I want to call it like <code>alpha("FOO", myp2)</code> and <code>myp2</code> will either contain a value to be passed, or be <code>None</code>. But even though the function handles <code>p2=None</code>, I want it to use its default value <code>"bar"</code> instead.<br>
  Maybe that's worded confusingly, so let me reword that:  </p>
  
  <blockquote>
    <p>If <code>myp2 is None</code>, call <code>alpha("FOO")</code>.   Else, call <code>alpha("FOO", myp2)</code>.</p>
  </blockquote>
  
  <p>The distinction is relevant because <code>alpha("FOO", None)</code> has a different result than <code>alpha("FOO")</code>.  </p>
  
  <p><strong>How can I concisely (but readably) make this distinction?</strong>  </p>
  
  <p>One possibility would usually be to <a href="https://stackoverflow.com/questions/16576553/python-only-pass-arguments-if-the-variable-exists">check for None within <code>alpha</code></a>, which would be encouraged because that would make the code safer. But assume that <code>alpha</code> is used in other places where it is actually supposed to handle <code>None</code> as it does.  </p>
  
  <p><strong>I'd like to handle that on the caller-side</strong>.  </p>
  
  <p>One possibility is to do a case distinction:</p>
  
  <pre><code>if myp2 is None:
      alpha("FOO")
  else:
      alpha("FOO", myp2)
  </code></pre>
  
  <p>But that can quickly become much code when there are multiple such arguments. (exponentially, 2^n)</p>
  
  <p>Another possibility is to simply do <code>alpha("FOO", myp2 or "bar")</code>, but <em>that requires us to know the default value</em>. Usually, I'd probably go with this approach, but I might later change the default values for <code>alpha</code> and this call would then need to be updated manually in order to still call it with the (new) default value.</p>
  
  <p>I am using python 3.4 but it would be best if your answers can provide a good way that works in any python version.</p>
  
  <hr>
  
  <p>The question is technically finished here, but I reword some requirement again, since the first answer did gloss over that:<br>
  I want the behaviour of <code>alpha</code> with its default values <code>"foo", "bar"</code> preserved in general, so it is (probably) not an option to change <code>alpha</code> itself.<br>
  In yet again other words, assume that <code>alpha</code> is being used somewhere else as <code>alpha("FOO", None)</code> where the output <code>FOO,None</code> is expected behaviour.</p>