I may be coming from a different mindset, being primarily a C++ programmer. This question has to do with OOP in Python and more specifically pure virtual methods. So taking code I adapted from this question I am looking at this basic sample.
class Animal():
def speak(self):
print("...")
class Cat(Animal):
def speak(self):
print("meow")
class Dog(Animal):
def speak(self):
print("woof")
my_pets = [Dog(), Cat(), Dog()]
for _pet in my_pets:
_pet.speak()
So you see it calls the speak function for different derived classes. Now my problem is that duck typing is all good and I think I have grasped it. However, is it wrong to pursue more strict OOP in Python? So I looked at the Abstract Base Classes and specifically abstractmethod. To me all this seems to do is allow me to call the base class method with super. Is there any way/reason(in Python) to make speak()
pure such that implementing a derived animal without speak would throw an error?
My argument for such a pursuit would be when writing modules and frameworks that you intend people to subclass, this would self document for them the fact that they need implement the function. A probably very bad idea is something like this, having the base class "pure" function throw an exception. Problem is that this error is found at runtime!
class VirtualException(BaseException):
def __init__(self, _type, _func):
BaseException(self)
class Animal():
def speak(self):
raise VirtualException()
class Cat(Animal):
def speak(self):
print("meow")
class Dog(Animal):
def speak(self):
print("woof")
class Wildebeest(Animal):
def function2(self):
print("What!")
my_pets = [Dog(), Cat(), Dog(), Wildebeest()]
for _pet in my_pets:
_pet.speak()
abstractmethod
does. Can you clarify what you want thatabstractmethod
doesn't provide? You're not going to get a "compile-time" solution for anything, because Python doesn't really have a compile time in the C++ sense. Butabstractmethod
lets you get the error when the class is defined (before it is instantiated). – Gissing