Why / How to use ABCMeta and abstractmethod

suggest change

Abstract base classes (ABCs) enforce what derived classes implement particular methods from the base class.

To understand how this works and why we should use it, let’s take a look at an example that Van Rossum would enjoy. Let’s say we have a Base class “MontyPython” with two methods (joke & punchline) that must be implemented by all derived classes.

class MontyPython:
    def joke(self):
        raise NotImplementedError()

    def punchline(self):
        raise NotImplementedError()

class ArgumentClinic(MontyPython):
    def joke(self):
        return "Hahahahahah"

When we instantiate an object and call it’s two methods, we’ll get an error (as expected) with the punchline() method.

>>> sketch = ArgumentClinic() 
>>> sketch.punchline() 
NotImplementedError

However, this still allows us to instantiate an object of the ArgumentClinic class without getting an error. In fact we don’t get an error until we look for the punchline().

This is avoided by using the Abstract Base Class (ABC) module. Let’s see how this works with the same example:

from abc import ABCMeta, abstractmethod

class MontyPython(metaclass=ABCMeta):
    @abstractmethod
    def joke(self):
        pass

@abstractmethod
def punchline(self):
    pass

class ArgumentClinic(MontyPython):
    def joke(self):
        return "Hahahahahah"

This time when we try to instantiate an object from the incomplete class, we immediately get a TypeError!

>>> c = ArgumentClinic()
TypeError:
"Can't instantiate abstract class ArgumentClinic with abstract methods punchline"

In this case, it’s easy to complete the class to avoid any TypeErrors:

class ArgumentClinic(MontyPython):
    def joke(self):
        return "Hahahahahah"

    def punchline(self):
        return "Send in the constable!"

This time when you instantiate an object it works!

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Abstract Base Classes:
* Why / How to use ABCMeta and abstractmethod

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
134 Abstract Base Classes
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve