title: How to use self in an abstract class implementation in Python?
body: <p>I'm working on a project using abstract classes in Python (specifically, the abc module).</p>
  <p>I have a few implementations of this abstract class, which have their own constructors and need to use <code>self</code>.</p>
  <p>This is what my code looks like, but simplified:</p>
  <pre><code>from abc import ABC, abstractmethod
  
  class BaseClass(ABC):
      def __init__(self):
          self.sublinks = [] # not meant to be passed in, that's why it isn't an argument in __init__
      
      @classmethod
      def display(cls):
          print(cls.get_contents())
      
      @abstractmethod
      def get_contents():
          pass
  
  class ImplementationOne(Base):
      def __init__(self, url):
          self.url = url
  
      def get_contents(self):
          return &quot;The url was: &quot; + url
  
  class ImplementationTwo(Base):
      def get_contents():
          return &quot;This does not need a url&quot;
  
  test_one = ImplementationOne(&quot;https://google.com&quot;)
  test_two = ImplementationTwo()
  
  test_one.display()
  </code></pre>
  <p>When I run this, however, I get the error <code>TypeError: get_contents() missing 1 required positional argument: 'self'</code>.</p>
  <p>I figured that this is because <code>get_contents()</code> in ImplementationOne takes <code>self</code>, but it's not specified in the abstract method.</p>
  <p>So, if I changed:</p>
  <pre><code>@abstractmethod
  def get_contents():
      pass
  </code></pre>
  <p>to</p>
  <pre><code>@abstractmethod
  def get_contents(self):
      pass
  </code></pre>
  <p>But I get the same error.</p>
  <p>I've tried many combinations, including putting <code>self</code> as an argument to every occurrence or <code>get_contents</code>, and passing in <code>cls</code> to <code>get_contents</code> in the abstract class - but no luck.</p>
  <p>So, pretty much, how can I use the <code>self</code> keyword (aka access attributes) in only some implementations of an abstract method, that's called within a class method in the abstract class itself.</p>
  <p>Also, on a side note, how can I access <code>self.sublinks</code> from within all implementations of BaseClass, while having its values different in each instance of an implementation?</p>