[Tutor] What are the benefits of template or abstract base classes?
boB Stepp
robertvstepp at gmail.com
Sun Jun 7 23:02:50 EDT 2020
Now in "4.2 Inheritance" at
https://dabeaz-course.github.io/practical-python/Notes/04_Classes_objects/02_Inheritance.html
class TableFormatter:
def headings(self, headers):
'''
Emit the table headings.
'''
raise NotImplementedError()
def row(self, rowdata):
'''
Emit a single row of table data.
'''
raise NotImplementedError()
with the comment:
"This class does nothing, but it serves as a kind of design specification
for additional classes that will be defined shortly. A class like this is
sometimes called an “abstract base class.”"
As the exercises in this section progressed I had to write the classes
TextTableFormatter(TableFormatter), CSVTableFormatter(TableFormatter) and
HTMLTableFormatter(TableFormatter) for which each class had its custom
implementations of the headings and row methods. I don't see much real
benefit to having the original TableFormatter class as used here. Am I
wrong to think this? At best it gives a single place to look to remind a
coder what methods are intended to be implemented for all subclasses, but
does not constrain the coder to do so.
OTOH, this inspired me to read up a bit on actual abstract base classes
(ABC), which *do* force the coder to implement the methods of the ABC or it
will not allow one to instantiate the method-deficient subclass. I can see
some benefit to this approach. Of course, Beazley may be planning on
heading in the true ABC direction later in the material.
What are your thoughts? Perhaps a fuller explanation for noobie boB?
--
Wishing you only the best,
boB Stepp
More information about the Tutor
mailing list