title: Best practice to implement copy method in a class?
body: <p>What is the best practice to implement the <code>copy</code> method in a class?</p>
  <p>Here is an example of my class:</p>
  <pre><code>class MyClass:
      def __init__(self, foo, bar=None):
          self.foo = foo
          if bar is not None:
              self.bar = bar
          else:
              self.bar = {'Hello': 'Ciao'}
  </code></pre>
  <p>I found a post of five years ago that suggests the following way:</p>
  <pre><code>import copy
  
  def copy(self):
      return MyClass(copy.copy(self.foo), copy.copy(self.bar))
  </code></pre>
  <p>Is it still the only way to do it or are there other possibilities?</p>
  <p>I need to create a copy of my object to avoid that a function changes the original object. The function is something like this:</p>
  <pre><code>def translate_and_print(my_class, dict={'Hello':'Ciao'}):
      temp = my_class # here I want to use the copy method: temp = my_class.copy()
      temp.foo = temp.bar[temp.foo]
      print(temp.foo)
  </code></pre>
  <p>The output of the following code is &quot;Ciao&quot;, &quot;Ciao&quot; but should be &quot;Ciao&quot;, &quot;Hello&quot;</p>
  <pre><code>mc = MyClass('Hello')
  translate_and_print(mc)
  print(mc.foo)
  </code></pre>
  <p>If I use the <code>copy()</code> method I have the error:</p>
  <blockquote>
  <p>AttributeError: 'MyClass' object has no attribute 'copy'</p>
  </blockquote>