Following this tutorial I'm trying to use Abstract Base Classes in Python. So I constructed two files:
basis.py:
import abc
class PluginBase(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def load(self, input):
return
and implementation.py:
import abc
from basis import PluginBase
class SubclassImplementation(PluginBase):
def load(self, input):
print input
return input
if __name__ == '__main__':
print 'Subclass:', issubclass(SubclassImplementation, PluginBase)
print 'Instance:', isinstance(SubclassImplementation(), PluginBase)
running python implementation.py
works fine, but I now want to use implementation.py
as a module for something else. So I get into the command line and do:
>>> from implementation import SubclassImplementation as imp
>>> imp.load('lala')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method load() must be called with SubclassImplementation instance as first argument (got str instance instead)
>>>
What am I doing wrong here, and how can I make this work? All tips are welcome!
You do need to create an instance:
from implementation import SubclassImplementation as imp
imp().load('lala')
This is not a specific ABC problem either; this applies to all Python classes and methods, unless you make SubclassImplementation.load
a classmethod
.