title: Pickle a dynamically parameterized sub-class
body: <p>I have a system which commonly stores pickled class types.</p>
  
  <p>I want to be able to save dynamically-parameterized classes in the same way, but I can't because I get a PicklingError on trying to pickle a class which is not globally found (not defined in simple code).</p>
  
  <p>My problem can be modeled as the following example code:</p>
  
  <pre><code>class Base(object):
   def m(self):
    return self.__class__.PARAM
  
  def make_parameterized(param_value):
   class AutoSubClass(Base):
    PARAM = param_value
   return AutoSubClass
  
  cls = make_parameterized(input("param value?"))
  </code></pre>
  
  <p>When I try to pickle the class, I get the following error:</p>
  
  <pre><code># pickle.PicklingError: Can't pickle &lt;class '__main__.AutoSubClass'&gt;: it's not found as __main__.AutoSubClass
  import pickle
  print pickle.dumps(cls)
  </code></pre>
  
  <p>I am looking for some method to declare Base as a <code>ParameterizableBaseClass</code> which should define the params needed (<code>PARAM</code> in above example). A dynamic parameterized subclass (<code>cls</code> above) should then be picklable by saving the "ParameterizableBaseClass" type and the different param-values (dynamic <code>param_value</code> above).</p>
  
  <p>I am sure that in many cases, this can be avoided altogether... And I can avoid this in my code as well if I really (really) have to. I was playing with <code>__metaclass__</code>, <code>copyreg</code> and even <code>__builtin__.issubclass</code> at some point (don't ask), but was unable to crack this one.</p>
  
  <p>I feel like I wouldn't be true to the python spirit if I wasn't to ask: how can this be achieved, in a relatively clean way?</p>