I have been reading up on the distinction between virtual vs abstract methods. This is well documented. Difference between virtual and abstract methods
Virtual methods have an implementation and provide the derived classes with the option of overriding it.
Abstract methods do not provide an implementation and force the derived classes to override the method.
So, abstract methods have no actual code in them, and subclasses HAVE TO override the method.
Based on these statements, it sounds like methods decorated with abc.abstractmethod
should have no implementation, only either pass
, ...
(used in abc
docs), or raise NotImplementedError
.
It seems programmers commonly use abc.abstractmethod
to implement a virtual function, where overriding is mandatory. This can be seen in many places:
- What should I put in the body of an abstract method in Python --> Mercurial > cpython
- In the Python 3.6
abc
docs under__subclasshook__
However, this seems to go against the very definition of the design pattern for abstract methods having no body.
This answer makes a good point on the subject matter.
My observation is the virtual vs abstract method design pattern is currently loosely followed in Python.
Can anyone shed light on this? What is the best practice?