title: Python: Inheritance versus Composition
body: <p>I am working with two classes in Python, one of which should be allowed to have any number objects from another class as children while keeping an inventory of these children as an attribute. Inheritance seemed like the obvious choice for this parent&lt;&gt;child situation but instead what I have arrived at is an example of composition. Here is the simplified code:</p>
  <pre class="lang-py prettyprint-override"><code>class Parent:
      def __init__(self, firstname, lastname):
          self.firstname = firstname
          self.lastname = lastname
          self.kids = []
  
      def havechild(self, firstname):
          print(self.firstname, &quot;is having a child&quot;)
          self.kids.append(Child(self, firstname))
  
  
  class Child(Parent):
      def __init__(self, parent, firstname):
          self.parent = parent
          self.firstname = firstname
          self.lastname = parent.lastname
  </code></pre>
  <p>So basically, while it seems to make intuitive sense to have Child() inherit from Parent(), removing the inheritance, does not change anything at all. The only benefit I can see for leaving <em>Child(Parent)</em> instead of just <em>class Child()</em> would be if I needed to add a lot more methods to <em>Parent</em> that I would like <em>Child</em> to inherit. Using the <em>self.parent = parent</em>, I already have access to any additional future attributes of the Parent.</p>
  <p>Is there another way to use pure inheritance rather than passing the <em>Parent</em> instance into the <em>Child</em> constructor (composition)?</p>