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>Your B class __init__ method is not taking any argument, while the __init__ of class A require you to pass one (named "a"), and yet, you are not providing it. Neither in your B class or by passing it to A.</p>

<p>However, this would work.</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):
        self.a = 10
        super(B, self).abc()
</code></pre>

<p>Or:</p>

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

inst = B()
inst.abc() # 10
</code></pre>

python python-2.7