title: Decorating method (class methods overloading)
body: <p>Inspired by Muhammad Alkarouri answer in <a href="https://stackoverflow.com/questions/3038033/what-are-good-uses-for-python3s-function-annotations">What are good uses for Python3&#39;s &quot;Function Annotations&quot;</a> , I want to do this <code>multimethod</code> for methods, not regular functions. However, when I do this</p>
  
  <pre><code>registry = {}
  
  class MultiMethod(object):
      def __init__(self, name):
          self.name = name
          self.typemap = {}
      def __call__(self, *args):
          types = tuple(arg.__class__ for arg in args) # a generator expression!
          function = self.typemap.get(types)
      if function is None:
          raise TypeError("no match")
      return function(*args)
  def register(self, types, function):
      if types in self.typemap:
          raise TypeError("duplicate registration")
      self.typemap[types] = function
  
  def multimethod(function):
      name = function.__name__
      mm = registry.get(name)
      if mm is None:
          mm = registry[name] = MultiMethod(name)
      types = tuple(function.__annotations__.values())
      mm.register(types, function)
      return mm
  
  class A:
  @multimethod
  def foo(self, a: int):
      return "an int"
  
  a = A() 
  print( a.foo( 1 ) ) 
  </code></pre>
  
  <p>I got this:</p>
  
  <pre><code>Traceback (most recent call last):
    File "test.py", line 33, in &lt;module&gt;
      print( a.foo( 1 ) )
    File "test.py", line 12, in __call__
      return function(*args)
  TypeError: foo() takes exactly 2 arguments (1 given)
  </code></pre>
  
  <p>Which seems to be expected, as explained in <a href="https://stackoverflow.com/questions/3401421/decorating-a-method">Decorating a method</a> , because of the <code>self</code> argument.</p>
  
  <p>But I have no idea how to make it work. Well, when I remove the "self", it's working (almost) fine, but I don't want to remove it. Please note, that I'm doing this for practice, I know that there are some libs, providing method overloading.</p>
  
  <p>What I tried:</p>
  
  <ul>
  <li><p>very silly, but wanted to try - added parameter <code>self</code> in <code>def multimethod( function )</code> - the same error</p></li>
  <li><p>I thought about adding in the <code>__init__</code> of <code>class MultiMethod</code> a third parameter - <code>obj</code> and stored <code>self</code> as member, but I can't do this through <code>multimethod</code> as it is a function.</p></li>
  <li><p>I don't want to add parameters for the decorator, so this options (if possible at all) is ignored</p></li>
  </ul>
  
  <p>I read several similar questions, but didn't find what I was looking for. I'm pretty sure this is dummy question, but I ran out of ideas.</p>