Test None for an object that does not implement ==

Chris Angelico rosuav at gmail.com
Sun Dec 25 08:35:46 EST 2011


On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith <roy at panix.com> wrote:
> Just for fun, I tried playing around with subclassing NoneType and
> writing an __eq__ for my subclass.  Turns out, you can't do that:
>
> Traceback (most recent call last):
>  File "./none.py", line 5, in <module>
>    class Nihil(NoneType):
> TypeError: Error when calling the metaclass bases
>    type 'NoneType' is not an acceptable base type

Yes; unfortunately quite a few Python built-in classes can't be
subclassed. It's an unfortunate fact of implementation, I think,
rather than a deliberate rule.

But then, what would you ever need to subclass None for, other than
toys and testing?

ChrisA



More information about the Python-list mailing list