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>I don't think there's any way to do this in the function signature itself. However, you could always have a check inside your function to see if both arguments are set</p>

<pre><code>def delete(title=None, pageid=None):
# Deletes the page given, either by its title or ID
    if title and pageid:
        # throw error or return an error value
pass
</code></pre>

<p>Arguably a better way of doing it would be to define 2 methods that call the delete method</p>

<pre><code>def delete_by_title(title):
    delete(title=title)

def delete_by_id(id):
    delete(pageid=id)
</code></pre>

<p>The second method won't stop people calling the delete function directly, so if that's really important to you I'd advise having it throw an exception as per my first example, or else a combination of the 2.</p>

python function arguments