I'm working on a kind of lib, and for a weird reason i have this error.
Sorry couldn't just copy and paste it
I went on the basis that the code below works
test.py
import abc import six @six.add_metaclass(abc.ABCMeta) class Base(object): @abc.abstractmethod def whatever(self,): raise NotImplementedError class SubClass(Base): def __init__(self,): super(Base, self).__init__() self.whatever() def whatever(self,): print("whatever")
In the python shell
>>> from test import * >>> s = SubClass() whatever
Why for my roster module i'm having this error
Can't instantiate abstract class Player with abstract methods _Base__json_builder, _Base__xml_builder
Thanks in advance
A class that is derived from an abstract class cannot be instantiated unless all of its abstract methods are overridden. You may think that abstract methods can't be implemented in the abstract base class. This impression is wrong: An abstract method can have an implementation in the abstract class!
An abstract class cannot be instantiated. It just provides an interface for subclasses to avoid code duplication. It makes no sense to instantiate an abstract class. A derived subclass must implement the abstract methods to create a concrete class that fits the interface defined by the abstract class.
We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.
Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
Your issue comes because you have defined the abstract methods in your base abstract class with __
(double underscore) prepended. This causes python to do name mangling at the time of definition of the classes.
The names of the function change from __json_builder
to _Base__json_builder
or __xml_builder
to _Base__xml_builder
. And this is the name you have to implement/overwrite in your subclass.
To show this behavior in your example -
>>> import abc >>> import six >>> @six.add_metaclass(abc.ABCMeta) ... class Base(object): ... @abc.abstractmethod ... def __whatever(self): ... raise NotImplementedError ... >>> class SubClass(Base): ... def __init__(self): ... super(Base, self).__init__() ... self.__whatever() ... def __whatever(self): ... print("whatever") ... >>> a = SubClass() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class SubClass with abstract methods _Base__whatever
When I change the implementation to the following, it works
>>> class SubClass(Base): ... def __init__(self): ... super(Base, self).__init__() ... self._Base__whatever() ... def _Base__whatever(self): ... print("whatever") ... >>> a = SubClass() whatever
But this is very tedious , you may want to think about if you really want to define your functions with __
(double underscore) . You can read more about name mangling here .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With