My Python application contains many abstract classes and implementations. For example:
import abc
import datetime
class MessageDisplay(object):
__metaclass__ = abc.ABCMeta
@abc.abstractproperty
def display(self, message):
pass
class FriendlyMessageDisplay(MessageDisplay):
def greet(self):
hour = datetime.datetime.now().timetuple().tm_hour
if hour < 7:
raise Exception("Cannot greet while asleep.")
elif hour < 12:
self.display("Good morning!")
elif hour < 18:
self.display("Good afternoon!")
elif hour < 20:
self.display("Good evening!")
else:
self.display("Good night.")
class FriendlyMessagePrinter(FriendlyMessageDisplay):
def display(self, message):
print(message)
FriendlyMessagePrinter
is a concrete class that we can use...
FriendlyMessagePrinter().greet()
Good night.
...but MessageDisplay
and FriendlyMessageDisplay
are abstract classes and attempting to instantiate one would result in an error:
TypeError: Can't instantiate abstract class MessageDisplay with abstract methods say
How can I check if a given class object is an (uninstantiatable) abstract class?