Python 3.5 - method overloading with @overload
<p>There is an <a href="https://pypi.python.org/pypi/overloading" rel="nofollow">overloading</a> package for Python 3.5+.
Whith this package, it's possible to redefined methods, but with distinct type hints and its decorator will find out, which overloaded method should be called. </p>

<p><strong>Common coding pattern:</strong></p>

<pre><code>class foo:
  def func(param):
    if instance(param, int):
      pass
    elif instance(param, str):
      pass
    elif instance(param, list):
      pass
    else:
      raise ValueError()
</code></pre>

<p><strong>With @overload:</strong></p>

<pre><code>class foo:
  @overload
  def func(param: int):
    pass

  @overload
  def func(param: str):
    pass

  @overload
  def func(param: list):
    pass
</code></pre>

<p>Here is the <a href="https://overloading.readthedocs.io/en/latest/" rel="nofollow">documentation</a>.</p>

<hr>

<p>My questions are:</p>

<ul>
<li>How big is the performance impact compared to old style parameter type switching?</li>
<li>And how does this package access the type hints?</li>
</ul>

<p>You'd have to measure it on your own with a real code. </p>

<p>I made a very quick look at the code of this library and conclusion is simple. It uses a lot of reflection (inspect package) and type comparison. inspect package on its own is mostly used by debugging tools - they always slow your code down. </p>

<p>Just look at these lines:</p>

<pre><code>complexity = complexity_mapping[id]
if complexity &amp; 8 and isinstance(arg, tuple):
     element_type = tuple(type(el) for el in arg)
elif complexity &amp; 4 and hasattr(arg, 'keys'):
     element_type = (type(element), type(arg[element]))
else:
     element_type = type(element)
</code></pre>

<h1> </h1>

<pre><code>type_hints = typing.get_type_hints(func) if typing else func.__annotations__
types = tuple(normalize_type(type_hints.get(param, AnyType)) for param in parameters)
</code></pre>

<p>Note that this package if over 7 months old and has only 70 stars. Python is not Java... You'd really hurt python itself with this package :D You'd better implement some core api method that delegates calls to other methods/objects, based on type parameters - just like it should be done with Python.  </p>

python overloading python-3.5 method-overloading