How to call deepcopy with super in python?
<p>I have 2 classes, let's call them Class1 and Class2(Class1), and Class2 derives from Class1. </p>

<p>In Class1, copy.deepcopy works very well and I <strong>don't</strong> want to implement a method <strong>deepcopy</strong> on Class1. </p>

<p>Now, I have an instance i2=Class2(someParameters). I want to make a deepcopy of i2 as in instance of Class1. </p>

<p>I cannot call copy.deepcopy(i2) because this would deepcopy i2 as in instance of class2 (with signature problems in my case).  </p>

<p>I cannot call super().<strong>deepcopy</strong>(i2) because <strong>deepcopy</strong> is not found in class1, and one falls back to the computation of copy.deepcopy(i2) as an instance of class2, and we go to an infinite loop. </p>

<p>How to proceed then to call the builtin copy.deepcopy(i2), regarding i2 as an instance of Class1 ?  </p>

<p>Thank you for your ideas, Laurent.</p>

<p>EDIT: As asked in the comments, an example of code to show the signature problem if I simply call copy.deepcopy</p>

<pre><code>import copy
from copy import deepcopy


class Class1(object):
    pass

class Class2(Class1):
    def __new__(cls,start):
        eClass1=Class1.__new__(cls)
        Class1.__init__(eClass1)
        eClass1.start=start
        return eClass1

instanceClass2=Class2(1)
copy.deepcopy(instanceClass2)
</code></pre>

<p>The answer from the interpreter:</p>

<pre><code>Traceback (most recent call last):
  File "essaiDebug7.py", line 18, in &lt;module&gt;
    copy.deepcopy(instanceClass2)
  File "/usr/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python2.7/copy.py", line 329, in _reconstruct
    y = callable(*args)
  File "/usr/lib/python2.7/copy_reg.py", line 93, in __newobj__
    return cls.__new__(cls, *args)
TypeError: __new__() takes exactly 2 arguments (1 given)
</code></pre>

<p>How about instead of inheriting <code>Class1</code>, create a object of <code>Class2 in Class1</code>? Like:</p>

<pre><code>class Class2(object):
    def __init__(self):
        self.class_1 = Class1()

# Then deepcopy
copy.deepcopy(class_2.class_1)
</code></pre>

<p><strong>Note</strong>: It would be great if you can share the real scenario. Because, based on the problem you mentioned, I do not think that your classes are inheritable. If they are, <em>I do not understand why you just want to copy parent class?</em></p>

python copy super