How the below program is possible in python
<p>I came with a situation where the method of class A to be called from class B.</p>

<pre><code>class A(object):
    def __init__(self, a):
        self.a = a

    def abc(self):
        print self.a

class B(A):
    def  __init__(self):
        super(B, self).abc()

def method1():
    a = A(2)

method1()
b = B()

Expecting Output: 2
</code></pre>

<p>Is it possible to call method 'abc' from class B with changing class A and should not create class A object in class B. If yes, then please let me know the solution.</p>

<p>The above program which I tried is giving error.</p>

<p>And the error I am getting is below</p>

<pre><code>Traceback (most recent call last):
   File "a.py", line 12, in &lt;module&gt;
       b = B()
   File "a.py", line 10, in __init__
       super(B, self).abc()
File "a.py", line 6, in abc
   print self.a
AttributeError: 'B' object has no attribute 'a'
</code></pre>

<p>Here:</p>

<pre><code>class B(A):
    def  __init__(self):
        super(B, self).abc()
</code></pre>

<p>The constructor of <code>A</code> is never called, so the initialization done in <code>A.__init__</code> is missing. It fails in <code>print self.a</code>, because there is no <code>a</code>.
The super constructor should be called.</p>

<p>Furthermore, <code>super(B, self).abc()</code> is the same as <code>self.abc()</code>.</p>

<p>If there was a method named <code>abc</code> defined in <code>B</code>, then <code>self.abc()</code> would call the method from <code>B</code>, whereas <code>super(B, self).abc()</code> would call the method from the superclass.</p>

<p>So, since those are the same, I would not use the ugly one. It just makes the code less readable.</p>

<p>With those two fixes:</p>

<pre><code>class B(A):
    def  __init__(self):
        super(B, self).__init__(1000)  # whatever default value
        self.abc()
</code></pre>

python python-2.7