title: Python: How do you specify the parameter type of a Class function to the current class?
body: <p>I'm writing a set of custom classes with inheritance using Python. Here is the code, simplified:</p>
  <pre><code>class Parent(object):
      __slots__ = ['a','b']
  
      def __init__(self, a:int, b:int):
          self.a = a
          self.b = b
      
      def myfunc(self,s)-&gt;int:
          return self.a + s.a
  
  class Child(Parent):
      __slots__ = ['c']
  
      def __init__(self, a:int, b:int, c:int):
          super().__init__(a,b)
          self.c = c
  
      def myfunc(self,s)-&gt;int:
          return self.a + s.a + s.c
  
  def main():
      dad = Parent(1,2)
      son = Child(4,8,16)
  
      print(dad.myfunc(son))
      print(son.myfunc(son))
      print(son.myfunc(dad))
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  <p>Upon running the function, I get this output (which I understand):</p>
  <pre><code>5
  24
  Error: Parent has no attribute 'c'
  </code></pre>
  <p>The myfunc() definition in Child requires that both self AND s are instances of Child.</p>
  <p>My ideal functionality for this code is that I want to implement method overloading. When <code>son.myfunc(dad)</code> is called, I want it to check the type of dad. If dad is also a <code>Child</code>, it should use <code>Child</code>'s definition of <code>myfunc()</code>. Otherwise, if dad isn't a <code>Child</code>, son should fall back to using the definition of <code>myfunc()</code> in <code>Parent</code>. So, for the above code, I'd want:</p>
  <pre><code>5
  24
  5
  </code></pre>
  <p>I would've thought that I could implement this functionality as follows, specifying the parameter types of <code>myfunc()</code>:</p>
  <pre><code>class Parent(object):
      __slots__ = ['a','b']
  
      def __init__(self, a:int, b:int):
          self.a = a
          self.b = b
      
      def myfunc(self, s:Parent)-&gt;int:
          return self.a + s.a
  
  class Child(Parent):
      __slots__ = ['c']
  
      def __init__(self, a:int, b:int, c:int):
          super().__init__(a,b)
          self.c = c
  
      def myfunc(self,s:Child)-&gt;int:
          return self.a + s.a + s.c
  
  def main():
      dad = Parent(1,2)
      son = Child(4,8,16)
  
      print(dad.myfunc(son))
      print(son.myfunc(son))
      print(son.myfunc(dad))
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  <p>However, this doesn't work. Python and my IDE tell me that <code>&quot;Parent is not defined&quot;</code> by the time I use it to specify the input type of <code>Parent's myfunc()</code>, and the same for <code>Child</code>. I think I get what it's saying ('<code>Parent</code>' won't mean anything until the compiler finishes reading Parent, so it can't be specified as the parameter type), but I don't know how to work around this.</p>
  <p>How can I implement my intended functionality? Is there some keyword like <code>'this'</code> or <code>'self'</code> that can be used to refer to the current class's type for these specifications? Even if method overloading doesn't work in this way- is there a way for a single class (<code>Parent</code>) to specify itself as the parameter type of one of its functions?</p>
  <p>Edit: I'm aware that Python parameter types are annotations for readability and error-checking, and they don't actually cause errors/failures if called with a different type. The second code segment was created to help communicate what I <strong>want</strong> to have occur. I'm aware I might have to add a check like isinstance() at the start of myfunc, but
  (a) I would like to avoid it if there is an alternative
  (b) if I did discover in the body of Child's myfunc() that I wanted to call the Parent's instead, I'm not certain of how I'd do that.</p>