title: How to override the copy/deepcopy operations for a Python object?
body: <p>I understand the difference between <code>copy</code> vs. <code>deepcopy</code> in the copy module. I've used <code>copy.copy</code> and <code>copy.deepcopy</code> before successfully, but this is the first time I've actually gone about overloading the <code>__copy__</code> and <code>__deepcopy__</code> methods.  I've already Googled around and looked through the built-in Python modules to look for instances of the <code>__copy__</code> and <code>__deepcopy__</code> functions (e.g. <code>sets.py</code>, <code>decimal.py</code>, and <code>fractions.py</code>), but I'm still not 100% sure I've got it right.</p>
  
  <p>Here's my scenario: </p>
  
  <p>I have a configuration object.  Initially, I'm going to instantiate one configuration object with a default set of values.  This configuration will be handed off to multiple other objects (to ensure all objects start with the same configuration).  However, once user interaction starts, each object needs to tweak its configurations independently without affecting each other's configurations (which says to me I'll need to make deepcopys of my initial configuration to hand around).</p>
  
  <p>Here's a sample object:</p>
  
  <pre><code>class ChartConfig(object):
  
      def __init__(self):
  
          #Drawing properties (Booleans/strings)
          self.antialiased = None
          self.plot_style = None
          self.plot_title = None
          self.autoscale = None
  
          #X axis properties (strings/ints)
          self.xaxis_title = None
          self.xaxis_tick_rotation = None
          self.xaxis_tick_align = None
  
          #Y axis properties (strings/ints)
          self.yaxis_title = None
          self.yaxis_tick_rotation = None
          self.yaxis_tick_align = None
  
          #A list of non-primitive objects
          self.trace_configs = []
  
      def __copy__(self):
          pass
  
      def __deepcopy__(self, memo):
          pass 
  </code></pre>
  
  <p>What is the right way to implement the <code>copy</code> and <code>deepcopy</code> methods on this object to ensure <code>copy.copy</code> and <code>copy.deepcopy</code> give me the proper behavior?</p>