Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>in normal python you can't do what you want. there are two close approximations:</p> <pre><code>def myfunction(first, second, *args): # args is a tuple of extra arguments def myfunction(first, second, third=None): # third is optional </code></pre> <p>however, if you <em>really</em> want to do this, you can certainly make it work (at the risk of offending the traditionalists ;o). in short, you would write a <code>wrapper(*args)</code> function that checks the number of arguments and delegates as appropriate. this kind of "hack" is usually done via decorators. in this case you could achieve something like:</p> <pre><code>from typing import overload @overload def myfunction(first): .... @myfunction.overload def myfunction(first, second): .... @myfunction.overload def myfunction(first, second, third): .... </code></pre> <p>and you'd implement this by making the <code>overload(first_fn)</code> <em>function</em> (or constructor) return a callable object where the <code>__call__(*args)</code> <em>method</em> does the delegation explained above and the <code>overload(another_fn)</code> <em>method</em> adds extra functions that can be delegated to.</p> <p>you can see an example of something similar here <a href="http://acooke.org/pytyp/pytyp.spec.dispatch.html" rel="noreferrer">http://acooke.org/pytyp/pytyp.spec.dispatch.html</a> but that is overloading methods by type. it's a very similar approach...</p> <p>UPDATE: and something similar (using argument types) is being added to python 3 - <a href="http://www.python.org/dev/peps/pep-0443/" rel="noreferrer">http://www.python.org/dev/peps/pep-0443/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload