There seem to be two reasons for the class to be "final" in Python.
1. Violation of class invariance
Classes that follow the Singleton pattern have an invariant that there is a limited (predefined) number of instances. Any violation of this invariant in a subclass will be incompatible with the intent of the class and will not work correctly. Examples:
bool : True , False ; see Guido commentsNoneType : NoneNotImplementedType : NotImplementedellipsis : ellipsis
There may be cases in this category other than the Singleton template, but I do not know about them.
2. There is no convincing use case.
A class implemented in C requires additional work to resolve a subclass (at least in CPython). Doing such work without a convincing use case is not very attractive, so volunteers are less likely to come forward. Examples:
Note 1:
Initially, I thought there were valid use cases, but just not enough interest, in the subclass of function and operator.itemgetter . Thanks to @agf, pointing out that the use cases here and here are not convincing (see @agf's comments on the question).
Note 2:
My concern is that another Python implementation may accidentally resolve a subclass of the class final in CPython. This can lead to non-portable code (the use case may be weak, but someone else can write code that subclasses function if their Python supports it). This can be resolved by noting in the Python documentation all the built-in and standard library classes that cannot be subclassed, and requiring that all implementations comply with CPython's behavior in this regard.
Note 3:
The message generated by CPython in all of the above cases:
TypeError: type 'bool' is not an acceptable base type
This is rather mysterious, as numerous questions on this topic show. I will send a suggestion to add a paragraph to the documentation explaining the final classes, and maybe even change the error message:
TypeError: type 'bool' is final (non-extensible)
max Apr 11 2018-12-12T00: 00Z
source share