title: Static classes being initialised on import. How does python 2 initialise static classes on import
body: <p>I am trying to introduce python 3 support for the package <a href="https://pypi.python.org/pypi/mime" rel="nofollow">mime</a> and the code is doing something I have never seen before. </p>
  
  <p>There is a class <code>Types()</code> that is used in the package as a static class. </p>
  
  <pre><code>class Types(with_metaclass(ItemMeta, object)): # I changed this for 2-3 compatibility
  
      type_variants = defaultdict(list)
      extension_index = defaultdict(list)
  
      # __metaclass__ = ItemMeta # unnessecary now
  
      def __init__(self, data_version=None):
          self.data_version = data_version
  </code></pre>
  
  <p>The <code>type_variants</code> defaultdict is what is getting filled in python 2 but not in 3. </p>
  
  <p>It very much seems to be getting filled by this class when is in a different file called <code>mime_types.py</code>.</p>
  
  <pre><code>class MIMETypes(object):
      _types = Types(VERSION)
  
      def __repr__(self):
          return '&lt;MIMETypes version:%s&gt;' % VERSION
  
      @classmethod
      def load_from_file(cls, type_file):
          data = open(type_file).read()
          data = data.split('\n')
          mime_types = Types()
          for index, line in enumerate(data):
              item = line.strip()
              if not item:
                  continue
              try:
                  ret = TEXT_FORMAT_RE.match(item).groups()
              except Exception as e:
                  __parsing_error(type_file, index, line, e)
  
              (unregistered, obsolete, platform, mediatype, subtype, extensions,
               encoding, urls, docs, comment) = ret
              if mediatype is None:
                  if comment is None:
                      __parsing_error(type_file, index, line, RuntimeError)
                  continue
              extensions = extensions and extensions.split(',') or []
              urls = urls and urls.split(',') or []
              mime_type = Type('%s/%s' % (mediatype, subtype))
              mime_type.extensions = extensions
              ...
              mime_type.url = urls
              mime_types.add(mime_type) # instance of Type() is being filled?
          return mime_types
  </code></pre>
  
  <p>The function <code>startup()</code> is being run whenever <code>mime_types.py</code> is imported and it does this. </p>
  
  <pre><code>def startup():
      global STARTUP
      if STARTUP:
          type_files = glob(join(DIR, 'types', '*'))
          type_files.sort()
          for type_file in type_files:
              MIMETypes.load_from_file(type_file) # class method is filling Types?
      STARTUP = False
  </code></pre>
  
  <p>This all seems pretty weird to me. The <code>MIMETypes</code> class first creates an instance of <code>Types()</code> on the first line. <code>_types = Types(VERSION)</code>. It then seems to do nothing with this instance and only use the <code>mime_types</code> instance created in the <code>load_from_file()</code> class method. <code>mime_types = Types()</code>. </p>
  
  <p>This sort of thing vaguely reminds me of javascript class construction. How is the instance <code>mime_types</code> filling <code>Types.type_variants</code> so that when it is imported like this.</p>
  
  <p><code>from mime import Type, Types</code></p>
  
  <p>The class's <code>type_variants</code> defaultdict can be used. And why isn't this working in python 3?</p>
  
  <p>EDIT: </p>
  
  <p>Adding extra code to show how <code>type_variants</code> is filled </p>
  
  <pre><code>(In "Types" Class)
  @classmethod
  def add_type_variant(cls, mime_type):
      cls.type_veriants[mime_type.simplified].append(mime_type)
  
  @classmethod
  def add(cls, *types):
      for mime_type in types:
          if isinstance(mime_type, Types):
              cls.add(*mime_type.defined_types())
          else:
              mts = cls.type_veriants.get(mime_type.simplified)
              if mts and mime_type in mts:
                  Warning('Type %s already registered as a variant of %s.',
                          mime_type, mime_type.simplified)
              cls.add_type_variant(mime_type)
              cls.index_extensions(mime_type)
  </code></pre>
  
  <p>You can see that <code>MIMETypes</code> uses the <code>add()</code> classmethod.</p>