Emulating Final classes in Python
Ethan Furman
ethan at stoneleaf.us
Tue Jan 17 02:25:58 EST 2017
On 01/16/2017 11:05 PM, Steven D'Aprano wrote:
> 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
>
>
> It doesn't have to be absolutely bulletproof, but anyone wanting to subclass my
> class should need to work for it, which hopefully will tell them that they're
> doing something unsupported.
>
> Any hints?
Use a metaclass. Have the metaclass create the first one, and refuse to make any others.
--
~Ethan~
More information about the Python-list
mailing list