title: Load order of objects in Python pickle
body: <p>I have a python pickle file and when I try to load it, I would like it to load one specific object before another (because I have some dependencies...).</p>
  
  <p>Is that possible?</p>
  
  <p>Here is the scenario:</p>
  
  <pre><code>class A:
  
    def __init__(self):
         self.known_names = ["Dan", "David"]
  
    def __getattr__(self, name):
          if name not in self.known_names:
             raise UnknownName
          else:
             return self[name]
  
  class B:
     def __init__(self):
         self.a_instance = A()
     def __setattr__(self, name, value):
         if self.a_instance.attr == "something":
            do_something...
     def __setstate__(self):
          self.foo = "blah"
  </code></pre>
  
  <p>The problem occurs when loading the pickle file. class B instance is loaded beofe class A. In that scenario, class B's <code>__setstate__</code> method tries to set <code>self.foo</code>. This results <code>__settattr__</code> method call which checks <code>self.a_instance</code>'s <code>attr</code> attribute. However, class A was not unpickled yet so <code>self.known_names</code> does not exist. So calling the <code>__getattr__</code> of A's results in an infinite recursion (since <code>known_names</code> does not exist, it calls <code>__getattr__</code> on it as well).</p>