How can I add to the initial definition of a python class inheriting from another class?
<p>I'm trying to define <code>self.data</code> inside a class inheriting from a class</p>

<pre><code>class Object():
    def __init__(self):
        self.data="1234"

class New_Object(Object):
    # Code changing self.data here
</code></pre>

<p>But I ran into an issue.</p>

<pre><code>class Object():
    def __init__(self):
        self.data="1234"
</code></pre>

<p>So I have the beginning class here, which is imported from elsewhere, and let's say that the class is a universal one so I can't modify the original at all.</p>

<p>In the original, the instance is referred to as "<code>self</code>" inside the class, and it is defined as self inside the definition <code>__init__</code>.</p>

<pre><code>class New_Object(Object):
    # Code changing self.data here
</code></pre>

<p>So if I wanted to inherit from the class <code>Object</code>, but define self.data inside <code>New_Object</code>, I thought I would have to define <code>__init__</code> in <code>New_Object</code>, but this overrides the <code>__init__</code> from <code>New_Object</code></p>

<p>Is there any way I could do this without copypasting the <code>__init__</code> from <code>Object</code>?</p>

<p>You can use <code>super().__init__()</code> to call <code>Object.__init__()</code> from <code>New_Object.__init__()</code>.</p>

<p>What you would do:</p>

<pre><code>class Object:
    def __init__(self):
        print("Object init")
        self.data = "1234"

class New_Object(Object):
    def __init__(self):
        print("calling super")
        super().__init__()
        print("data is now", self.data)
        self.data = self.data.split("3")

o = New_Object()

# calling super
# Object init
# data is now 1234
</code></pre>

<p>Note that you do not have to give any arguments to <code>super()</code>, as long as you are using Python 3.</p>

python class inheritance instance