Followingthis tutorial I'm trying to use Abstract Base Classes in Python. So I constructed two files:
basis.py:
import abcclass PluginBase(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def load(self, input): return
andimplementation.py:
import abcfrom basis import PluginBaseclass SubclassImplementation(PluginBase): def load(self, input): print input return inputif __name__ == '__main__': print 'Subclass:', issubclass(SubclassImplementation, PluginBase) print 'Instance:', isinstance(SubclassImplementation(), PluginBase)
runningpython implementation.py
works fine, but I now want to useimplementation.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!
2 Answers2
You do need tocreate an instance:
from implementation import SubclassImplementation as impimp().load('lala')
This is not a specific ABC problem either; this applies to all Python classes and methods, unless you makeSubclassImplementation.load
aclassmethod
.
1 Comment
This doesn't have anything to do with ABCs.
The object you've calledimp
is a class, ie SubclassImplementation. You'll need to instantiate it before you can call any of its instance methods. Alternatively, you could makeload
a classmethod.
1 Comment
Explore related questions
See similar questions with these tags.