A class subclass of itself. Why mutual subclassing is forbidden?

后端 未结 6 634
天命终不由人
天命终不由人 2021-01-12 21:25

Complex question I assume, but studying OWL opened a new perspective to live, the universe and everything. I\'m going philosophical here.

I am trying to achieve a cl

6条回答
  •  深忆病人
    2021-01-12 22:04

    Python doesn't allow it because there is no sensible way to do it. You could invent arbitrary rules about how to handle such a case (and perhaps some languages do), but since there is no actual gain in doing so, Python refuses to guess. Classes are required to have a stable, predictable method resolution order for a number of reasons, and so weird, unpredictable or surprising MROs are not allowed.

    That said, there is a special case in Python: type and object. object is an instance of type, and type is a subclass of object. And of course, type is also an instance of type (since it's a subclass of object). This might be why OWL allows it: you need to start a class/metaclass hierarchy in some singularity, if you want everything to be an object and all objects to have a class.

提交回复
热议问题