Emulating Final classes in Python
ethan at stoneleaf.us
Tue Jan 17 23:42:28 EST 2017
On 01/17/2017 01:37 AM, Antoon Pardon wrote:
> Op 17-01-17 om 08:05 schreef Steven D'Aprano:
>> I wish to emulate a "final" class using Python, similar to bool:
>> py> class MyBool(bool):
>> ... pass
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: type 'bool' is not an acceptable base type
> I find those kind of classes annoying as hell and nobody has ever given me a good
> reason for them.
Subclassing an Enum that has members can be confusing -- subclassed members pass isinstance checks but fail to show up in the parent class' iteration.
Subclassing a new data type, such as one with Yes, No, and Maybe, with lots of code dealing explicitly with those three singletons, would be confusing if not outright broken.
Both those cases are good candidates for disallowing subclassing.
More information about the Python-list