title: Best way to represent range of numbers in a set
body: <p>I am trying to create a class which holds a bunch of integers. I am using <code>set</code> to do the same. But I would like to compress the memory footprint by storing continuous ranges in some other form, may  be a range object. But in such a case, how will I implement functionalities like <code>union</code>, <code>intersection</code>, <code>issubset</code> etc?</p>
  
  <p>Below is my code as of now. In <code>__init__</code> I would like to implement a compress feature so that continuous ranges are not stored as is. Also, I would like to accept ranges as part of the items.</p>
  
  <pre><code>class Component(object):
      """
      Component class
  
      Attributes:
          name (str): The name of the component.
          type (str): The type of the component.
          items (set): The members of the component.
      """
  
      def __init__(self, comp_name, comp_type, comp_items):
          """
          Constructor for the Component object.
  
          Args:
              comp_name (str): Name of the component.
              comp_type (str): Type of the component.
              comp_items (iterable): An iterable of the members of the component.
          """
          self.name = comp_name
          self.type = comp_type.lower()
          if not all(isinstance(x, int) for x in comp_items):
              raise TypeError("All items in the component items should of type int")
          self.items = set(comp_items)
  
      @property
      def count(self):
          """Return the number of items in the component"""
          return len(self.items)
  
      def max(self):
          """Returns the max ID number from the component items"""
          return max(self.items)
  
      def min(self):
          """Returns the min ID number from the component items"""
          return min(self.items)
  
      def __repr__(self):
          """Representation of the component"""
          return "&lt;Component Object-&gt; Name:{}, Type:{}, Count: {}&gt;".format(
              self.name, self.type, self.count
          )
  
      def __len__(self):
          """len() function on the Component object"""
          return len(self.items)
  
      def __eq__(self, other):
          """Checking if one component is equal to another"""
          return self.type == other.type and self.items == other.items
  
      def __and__(self, other):
          """Intersection of two components"""
          assert self.type == other.type
          return self.__class__("", self.type, self.items &amp; other.items)
  
      def __or__(self, other):
          """Union of two components"""
          assert self.type == other.type
          return self.__class__("", self.type, self.items | other.items)
  
      def __add__(self, other):
          """Adding two components together"""
          return self.__or__(other)
  
      def __sub__(self, other):
          """Removing one Component from another"""
          assert self.type == other.type
          return self.__class__("", self.type, self.items - other.items)
  
      def issubset(self, other):
          """Checking if self is a subset of other"""
          return self.items.issubset(other.items)
  
      def __contains__(self, other):
          return self.items.__contains__(other)
  </code></pre>