Python function that takes one compulsory argument from two choices
<p>I have a function:</p>

<pre><code>def delete(title=None, pageid=None):
    # Deletes the page given, either by its title or ID
    ...
    pass
</code></pre>

<p>However, my intention is for the function to take only one argument (either <code>title</code> OR <code>pageid</code>). For example, <code>delete(title="MyPage")</code> or <code>delete(pageid=53342)</code> are valid, while <code>delete(title="MyPage", pageid=53342)</code>, is not. Zero arguments can not be passed. How would I go about doing this?</p>

<p>If both arguments can have any value, including <code>None</code>, then the solution is to use a sentinel object for them. Then you can calculate a sum over the  number of arguments that are set to non-default value:</p>

<pre><code>NOT_SET = object()
def delete(title=NOT_SET, pageid=NOT_SET):
    if sum(i is not NOT_SET for i in [title, page_id]) != 1:
        raise ValueError('Can set only one of these')
</code></pre>

<p>This pattern is easily expandable to more arguments as well.</p>

python function arguments