Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Abstract Base Class without ABC

class NotImplementedError(Exception):
    pass

class Base():
    def foo(self):
        raise NotImplementedError()

    def bar(self):
        raise NotImplementedError()

class Real(Base):
    def foo(self):
        print('foo in Real')
    def bar(self):
        print('bar in Real')
    def other(self):
        pass

class Fake(Base):
    def foo(self):
        print('foo in Fake')

r = Real()
r.foo()
r.bar()
f = Fake()
f.foo()
f.bar()
foo in Real
bar in Real
foo in Fake
Traceback (most recent call last):
  File "no_abc.py", line 28, in <module>
    f.bar()    # NotImplementedError
  File "no_abc.py", line 9, in bar
    raise NotImplementedError()
__main__.NotImplementedError