title: Subclassing namedtuple vs implementing __slots__?
body: <p>I'm currently trying to optimize a program that could benefit from a lesser memory footprint. Within that program are objects that store data and don't ever change their state, but they do however have functionality, for example:</p>
  
  <pre><code>class Extent:
      def __init__(self, xmin, ymin, xmax, ymax):
          self.xmin = xmin
          self.ymin = ymin
          self.xmax = xmax
          self.ymax = ymax
          self.center = ((xmin + xmax) / 2, (ymin + ymax) / 2)
      def contains(self, extent: 'Extent'):
          # checks if one contains the other
      def intersects(self, extent: 'Extent'):
          # checks if one intersects the other
  </code></pre>
  
  <p>In my search for memory savings, I came across <code>__slots__</code> which prevents classes from creating <code>__dict__</code> for arbitrary attribute assignment. This is great since I know what attributes the objects will have, so I can just add it in:</p>
  
  <pre><code>class Extent:
      __slots__ =  ('xmin', 'ymin', 'xmax', 'ymax', 'center')
      def __init__(self, xmin, ymin, xmax, ymax):
          ...
  </code></pre>
  
  <p>Then I started to explore <code>namedtuple</code>'s since they use a pretty small memory footprint, the downside is that I cannot add methods unless I subclass them:</p>
  
  <pre><code>Boundary = namedtuple('Boundary', ['xmin', 'ymin', 'xmax', 'ymax', 'center'])
  class Extent(Boundary):
      def contains(self, extent: 'Extent'):
          ...
      def intersects(self, extent: 'Extent'):
          ...
  </code></pre>
  
  <p>But this will still allow for <code>__dict__</code> to be created, so we must explicitly state <code>__slots__</code>:</p>
  
  <pre><code>Boundary = namedtuple('Boundary', ['xmin', 'ymin', 'xmax', 'ymax', 'center'])
  class Extent(Boundary):
      __slots__ = ()
      ...
  </code></pre>
  
  <p>So the question I have is what benefit is there to the last example (<code>namedtuple</code> subclass with <code>__slots__</code>) over the class that implements <code>__slots__</code> and doesn't inherit from anything?</p>