0

I am trying to use an abstract base class using the abc.ABCMeta . Here is the sample code -

import abc
class A(metaclass=abc.ABCMeta):
 def __init__(self):
 self.v1 = 10
 self.v2 = 10
 @abc.abstractmethod
 def first_method(self):
 pass
class B(A):
 def __init__(self):
 self.v2= 20
 def first_method(self):
 pass
if __name__ == '__main__':
 b = B()
 print("v1 =%d", b.v1)
 print("v2=%d", b.v2)

If I do not define __init__ in class B, it would just take the values of v1 and v2 from the superclass A. But,I want to use the value of v1 from the abstract class A and override the value of variable v2. If I try to call super.__init__(self) it gives an error. Can someone please let me know the way to accomplish this? Thank you.

UPDATE: Got the following error message with the above sample code: AttributeError: 'B' object has no attribute 'v1'

asked Aug 31, 2015 at 18:07
5
  • What does the error say :) ? Commented Aug 31, 2015 at 18:10
  • sorry, it says AttributeError: 'B' object has no attribute 'v1' Commented Aug 31, 2015 at 18:11
  • 3
    I meant with super().__init__(self) included? It probably says __init__() takes 1 positional argument but 2 were given. Try without the self parameter, its already included in the super() call. Commented Aug 31, 2015 at 18:13
  • ha! you got it... super().__init__() please post the answer so that i can accept :-) Commented Aug 31, 2015 at 18:16
  • You can pick Yaroslav's answer, no need to double post it :) Commented Aug 31, 2015 at 18:17

2 Answers 2

4

You should use super().__init__() instead of super.__init__(self).

answered Aug 31, 2015 at 18:15
Sign up to request clarification or add additional context in comments.

Comments

0

this should work.

import abc
class A(metaclass=abc.ABCMeta):
 def __init__(self):
 self.v1 = 10
 self.v2 = 10
 @abc.abstractmethod
 def first_method(self):
 pass
class B(A):
 def __init__(self):
 super(B, self).__init__()
 self.v2 = 20
 def first_method(self):
 pass
if __name__ == '__main__':
 b = B()
 print('v1 = ', b.v1)
 print('v2 = ', b.v2)
answered Aug 31, 2015 at 18:23

5 Comments

This is essentially the same answer as Yaroslav, except it doesn't work with Python 3. (It would be correct for Python 2, but that's not what the OP is using.)
No, it doesn't. Compare type(A) in Python 2 and 3; Python 3 doesn't look at the __metaclass__ attribute in the definition, so A isn't an abstract base class.
@chepner, you're right...now it is Python3 compatible
It is, but you aren't adding anything relevant that Yaroslav's answer doesn't already cover.
I did not notice it, until I posted.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.