0

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!

Martijn Pieters's user avatar
Martijn Pieters
1.1m325 gold badges4.2k silver badges3.4k bronze badges
askedMar 12, 2014 at 13:17
kramer65's user avatar
0

2 Answers2

3

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.

answeredMar 12, 2014 at 13:20
Martijn Pieters's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

As I said to Daniel as well, I've been working way to much with php static functions lately (bLuh, I love Python!) that I totally forgot this. Thanks anyway!
3

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.

answeredMar 12, 2014 at 13:21
Daniel Roseman's user avatar

1 Comment

How stupid of me. I have been working a lot with Static functions in php lately, so I got so used to directly running functions from a class, that I just forgot about instances. Thanks anyway!

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.