问题
What I want to achieve is to register one type as subtype of all other types. For some other reason I cannot use metaclass, so __init_subclass__
seems like a reasonable choice.
I have code like this
from abc import ABC
class AnyData(ABC):
pass
class BaseData(ABC):
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.register(AnyData)
class DataA(BaseData):
pass
However issubclass(AnyData, DataA)
returns False
, until the DataA
is subclassed, like
class DataB(DataA):
pass
Now, issubclass(AnyData, DataA)
returns True
and issubclass(AnyData, DataB)
returns False
Please help to reveal the reason why it behaves like this?
来源:https://stackoverflow.com/questions/59638421/register-subclass-to-an-abc-class-inside-init-subclass-does-not-fully-work