title: Help with copy and deepcopy in Python
body: <p>I think I tried to ask for far too much in <a href="https://stackoverflow.com/questions/2970456/trouble-with-copying-dictionaries-and-using-deepcopy-on-an-sqlalchemy-orm-object">my previous question</a> so apologies for that. Let me lay out my situation in as simple a manner as I can this time.</p>
  
  <p>Basically, I've got a bunch of dictionaries that reference my objects, which are in turn mapped using SQLAlchemy. All fine with me. However, I want to make iterative changes to the contents of those dictionaries. The problem is that doing so will change the objects they reference---and using copy.copy() does no good since it only copies the references contained within the dictionary. Thus even if copied something, when I try to, say <code>print</code> the contents of the dictionary, I'll only get the latest updated values for the object.</p>
  
  <p>This is why I wanted to use copy.deepcopy() but that does not work with SQLAlchemy. Now I'm in a dilemma since I need to copy certain attributes of my object before making said iterative changes.</p>
  
  <p>In summary, I need to use SQLAlchemy <b>and</b> at the same time make sure I can have a copy of my object attributes when making changes so I don't change the referenced object itself.</p>
  
  <p>Any advice, help, suggestions, etc.?</p>
  
  <hr>
  
  <p><code>Edit:</code> Have added some code.</p>
  
  <pre><code>class Student(object):
      def __init__(self, sid, name, allocated_proj_ref, allocated_rank):
          self.sid = sid
          self.name = name
          self.allocated_proj_ref = None
          self.allocated_rank = None
  
  students_table = Table('studs', metadata,
      Column('sid', Integer, primary_key=True),
      Column('name', String),
      Column('allocated_proj_ref', Integer, ForeignKey('projs.proj_id')),
      Column('allocated_rank', Integer)
  )
  
  mapper(Student, students_table, properties={'proj' : relation(Project)})
  
  students = {}
  
  students[sid] = Student(sid, name, allocated_project, allocated_rank)
  </code></pre>
  
  <p>Thus, the attributes that I will be changing are the <code>allocated_proj_ref</code> and <code>allocated_rank</code> attributes. The <code>students_table</code> is keyed using the unique student ID (<code>sid</code>).</p>
  
  <hr>
  
  <p><code>Question</code></p>
  
  <p>I'd want to persist the attributes I change above -- I mean, that's basically why I decided to use SQLA. However, the mapped object will be changing, which is not recommended. Thus, if I make the changes to doppelgänger, <b>unmapped</b> object... can I take those changes and update the fields/table for the <b>mapped</b> object.</p>
  
  <p>In a sense I'm following David's <a href="https://stackoverflow.com/questions/2983275/help-with-copy-and-deepcopy-in-python/2983310#2983310">secondary solution</a> where I create another version of the Class that isn't mapped.</p>
  
  <hr>
  
  <p>I tried using the <code>StudentDBRecord</code> solution mentioned below but got an error!</p>
  
  <pre><code>File "Main.py", line 25, in &lt;module&gt;
      prefsTableFile = 'Database/prefs-table.txt')
  File "/XXXX/DataReader.py", line 158, in readData
  readProjectsFile(projectsFile)
  File "/XXXX/DataReader.py", line 66, in readProjectsFile
  supervisors[ee_id] = Supervisor(ee_id, name, original_quota, loading_limit)
  File "&lt;string&gt;", line 4, in __init__
  raise exc.UnmappedClassError(class_)
  sqlalchemy.orm.exc.UnmappedClassError: Class 'ProjectParties.Student' is not mapped
  </code></pre>
  
  <p>Does this mean that <code>Student</code> <em>must</em> be mapped?</p>
  
  <hr>
  
  <p><code>Health warning!</code></p>
  
  <p>Someone pointed out a really good additional issue here. See, even if I'm calling <code>copy.deepcopy()</code> on a non-mapped object, in this case, let's assume it's the students dictionary I've defined above, deepcopy makes a copy of <em>everything</em>. My <code>allocated_proj_ref</code> is actually a <code>Project</code> object, and I've got a corresponding <code>projects</code> dictionary for that.</p>
  
  <p>So I deepcopy both <code>students</code> and <code>projects</code> -- which I am -- he says I'll have cases where the <code>students</code>'s <code>allocated_proj_ref</code> attribute will have issues with matching with instances in the <code>projects</code> dictionary.</p>
  
  <p>Thus, I take it that I'll have to redefine/override (that's what it's called isn't it?) <code>deepcopy</code> in each Class using <code>def __deecopy__(self, memo):</code> or something like that?</p>
  
  <hr>
  
  <p>I'd I'd like to override <code>__deepcopy__</code> such that it ignores all the SQLA stuff (which are <code>&lt;class 'sqlalchemy.util.symbol'&gt;</code> and <code>&lt;class 'sqlalchemy.orm.state.InstanceState'&gt;</code>) but copy everything else that's part of the a mapped class.</p>
  
  <p>Any suggestions, please?</p>