2

I am trying to create a python (3.5) class structure where I use abstract methods to indicate which methods should be implemented. The following works as expected:

from abc import ABC, abstractmethod
class Base(ABC):
    @abstractmethod
    def foo(self):
        pass

class Derived(Base, object):
    # Inheriting from object probably is superfluous (?)
    pass

Derived()  # throws a TypeError

When I add a class to Derived, it does not work anymore. Here shown with a tuple (in my specific case I want to use a np.ndarray):

class Base(ABC):
    @abstractmethod
    def foo(self):
        pass

class Derived(Base, tuple):
    pass

Derived()  # does not throw an error

Are abstract base classes in python not intended for multiple inheritance? Of course I could add old-school NotImplementedErrors, but then an error is only thrown when the method is called.

Aaron
  • 321
  • 4
  • 9
  • 1
    Does this answer your question? [python abstractmethod with another baseclass breaks abstract functionality](https://stackoverflow.com/questions/37398966/python-abstractmethod-with-another-baseclass-breaks-abstract-functionality) – Tom Wojcik Feb 01 '20 at 10:52
  • Thanks, I missed that. I guess the only solutions for the moment are a modified `__new__` or the NotImplementedError workaround. – Aaron Feb 01 '20 at 11:56
  • no problem. This was a super interesting edge case. I spent 30 min trying to figure out how to handle this problem and couldn't figure out a clean solution. Did you come up with something clean? – Tom Wojcik Feb 04 '20 at 16:30
  • Sure, since this question is locked as a duplicate, I posted an answer [here](https://stackoverflow.com/a/60125441/5814701). – Aaron Feb 08 '20 at 09:20

0 Answers0