Since this question was originally asked, python has changed how abstract classes are implemented. I have used a slightly different approach using the abc.ABC formalism in python 3.6. Here I define the constant as a property which must be defined in each subclass.
from abc import ABC, abstractmethod
CONSTANT = 42
This forces the derived class to define the constant, or else a
TypeError exception will be raised when you try to instantiate the subclass. When you want to use the constant for any functionality implemented in the abstract class, you must access the subclass constant by
type(self).CONSTANT instead of just
CONSTANT, since the value is undefined in the base class.
There are other ways to implement this, but I like this syntax as it seems to me the most plain and obvious for the reader.
The previous answers all touched useful points, but I feel the accepted answer does not directly answer the question because
- The question asks for implementation in an abstract class, but the accepted answer does not follow the abstract formalism.
- The question asks that implementation is enforced. I would argue that enforcement is stricter in this answer because it causes a runtime error when the subclass is instantiated if
CONSTANT is not defined. The accepted answer allows the object to be instantiated and only throws an error when
CONSTANT is accessed, making the enforcement less strict.
This is not to fault the original answers. Major changes to the abstract class syntax have occurred since they were posted, which in this case allow a neater and more functional implementation.