Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

class

owl

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 class C which is subclass of B which in turn is subclass of C. Just for fun, you know...

So here it is

>>> class A(object): pass
... 
>>> class B(A): pass
... 
>>> class C(B): pass
... 
>>> B.__bases__
(<class '__main__.A'>,)
>>> B.__bases__ = (C,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a __bases__ item causes an inheritance cycle
>>> 

clearly, python is smart and forbids this. However, in OWL it is possible to define two classes to be mutual subclasses. The question is: what is the mind boggling explanation why this is allowed in OWL (which is not a programming language) and disallowed in programming languages ?

like image 333
Stefano Borini Avatar asked Feb 08 '10 16:02

Stefano Borini


People also ask

Is a class A subclass of itself?

Is a class a subclass of itself? A class cannot extend itself since it IS itself, The definition of subclass is that it extends another class and inherits the state and behaviors from that class. so it is not a subclass.

What does a subclass inherit from a superclass?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Which declaration prevents creating a subclass of a top level Dass?

You can prevent a class from being subclassed by using the final keyword in the class's declaration.

What is the function used to find if a class A is a subclass of another class B?

Python issubclass() is built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of given class else it returns False.


2 Answers

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.

like image 79
Thomas Wouters Avatar answered Sep 22 '22 12:09

Thomas Wouters


The MRO scheme implemented in Python (as of 2.3) forbids cyclic subclassing. Valid MRO's are guaranteed to satisfy "local precedence" and "monotonicity". Cyclic subclassing would break monotonicity.

This issue is discussed in the section entitled "Bad Method Resolution Orders"

like image 37
unutbu Avatar answered Sep 18 '22 12:09

unutbu