title: Self replacing object python
body: <p>I would like to create an object that is capable of replacing itself with an arbitrary object when accessed; as in the following:</p>
  
  <pre><code>class stand_in_object(object):
      def __init__(self,obj_ref):
          self.obj_ref
  
      def __getattr__(self,key):
          pass
          #somehow make calling reference now refer to self.obj_ref
          #Consider: setattr(ref,name,self.obj_ref) - how do I get ref,name?
          #Or perhaps something that replaces the object located at "location"
          #so that all references id(self) are "pointing" to
          #a new object. Maybe ctypes.memmove?
  
  #------------------------Example use----------------------
  class x(object):
      def __init__(self):
          self.y=stand_in_object(int(5))
  
  a=x()
  print a.x.f #calls a.x.__getattr__('f') which sets a.x = int(5)
  </code></pre>
  
  <p>I was thinking I could try either the memmove, or replace all references method found this post: <a href="https://benkurtovic.com/2015/01/28/python-object-replacement.html" rel="nofollow noreferrer">https://benkurtovic.com/2015/01/28/python-object-replacement.html</a></p>
  
  <p>I'm attempting to make an OODB using cPickle. I would like to have persistent objects that have not been loaded, be referenced by a stand-in object until they are accessed. Something like the following:</p>
  
  <pre><code>loaded = {}
  class stand_in(object):
      def __init__(self,id,src): 
          self.id,self.src = id,src
      def __getattr__(self,key):
          loaded[self.id] = cust_unpickle(self.src)
      #Some function to replace reference of standin_obj with loaded object
  
  def persistent_load(id):
      if id in loaded: return loaded[id]
      else: return stand_in(id,src)
  
  #--------------------------Example-------------------------------
  #consider database stores objects a and c, and a.c = c.
  a = unpickle(a.id,src) #loaded = {a.id:a}, a.c = stand_in(c.id,c.src)
  print a.c.x  #calls a.c.__gettattr__ which has the effect:
               #c = unpickle(c.id,src)
               #and loads = {a.id:a,c.id:c}
  </code></pre>