Register subclass to an ABC class inside __init_subclass__ does not fully work

。_饼干妹妹 提交于 2020-06-16 07:56:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!