wim
wim

Reputation: 362826

How to use the __subclasscheck__ magic method?

How can we make a class who lies about who he has subclassed?

After reading the doc I've attempted this:

>>> class AllYourBase(type):
...     @classmethod
...     def __subclasscheck__(cls, other):
...         return True
...     
>>> class AllYour(object):
...     __metaclass__ = AllYourBase

Now, this class should report that all your base are belong to him.

But it didn't work:

>>> issubclass(AllYour, int)
False

Why not?

Upvotes: 1

Views: 1007

Answers (1)

user2357112
user2357112

Reputation: 281151

If you want AllYour to claim to be a subclass of every class, that isn't possible. __subclasscheck__ works in the other direction.

If you want AllYour to claim that every class subclasses it, remove the @classmethod decorator, and switch the arguments in the issubclass call. Special methods you define on a metaclass don't need special decoration.

Upvotes: 3

Related Questions