This is a corner I have painted myself into a few times. I would have thought given how common alternate constructors are in Python that it would not be unheard of to construct a subclass from an alternate constructor of its superclass, but I can't think of how to do that or find a discussion about it online.
To illustrate the problem. Say I have the following toy superclass.
class Parent:
    def __init__(self):
        self.x = 0
    @classmethod
    def from_x(cls, x):
        a = cls()
        a.x = x
        return a
Now say that I want to subclass Parent using its from_x method. I can think of four unsatisfying solutions.
Calling the superclass's alternate constructor in the subclass's
__init__as shown below fails becausefrom_xdoes not modify its input like__init__but returns a value more like__new__.# failed solution class Child(Parent): def __init__(self): super().from_x(1)Calling the superclass's
from_xmethod in the subclass's__new__as shown below also fails because both methods call each other circularly.# failed solution class Child(Parent): def __new__(self): return super().from_x(1)Overriding the superclass's
from_xmethod in the subclass makes the subclass's important__init__method vestigial and makes it assign the wrong value to x.# bad solution class Child(Parent): @classmethod def from_x(cls): return super().from_x(1)Giving up on inheritance and using composition instead works, but adds an unhelpful layer of syntax (i. e.
child.parent.x).# unfortunate compromise solution class Child: def __init__(self): self.parent = Parent.from_x(3)
Did I miss a better option for this goal?