
On Mon, 4 Apr 2022 at 20:50, Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Apr 04, 2022 at 07:17:55PM +1200, Greg Ewing wrote:
On 4/04/22 9:35 am, malmiteria wrote:
And just to make it clear, I am not under the impression super can only target one class. What i'm saying here is that it is the most common impression the average developper would have.
How do you know this? Have you undertaken a survey of randomly selected average developers?
I think that malmiteria is probably correct that most developers misunderstand or misuse super, or find it surprising. I know I do. Now matter how many times I remind myself that calling super().method is **not** the same as "call the method of my superclass", I still get surprised by how it actually works in the complicated cases.
Simple cases (single inheritence) are fine. But I'm going to be brave, or possibly foolhardy, and say that when it comes to multiple inheritence, if you think you understand what super does in all the fine details, you're either superhuman or wrong :-)
super().human()
In SI, calling super() is effectively the same as calling the superclass, and its all fine. But in MI, calling super() can resolve to something unexpected.
IMO the best way to think about what super does is actually to not think about what it precisely does. All that matters is: pass the call along. For that to be meaningful, two things must be true: 1) There is an endpoint that does not call super (this might be object() or some class of your own) 2) Every other class inherits from this endpoint class (implicitly, in the case of object) and has a compatible signature for the methods in question. One example is an __init__ method. Every method is defined thus: def __init__(self, *, spam="ham", **args): super().__init__(**args) # use the arg(s) that you defined Do you care which class is called next? No - each class is completely independent. You can pass extra args up the line if you want to, or look at an arg and still pass it along, or whatever's necessary, but the specific order of classes should be basically irrelevant unless two of them have their own dependency ordering (which would be defined by one inheriting from the other, thus guaranteeing the resolution order).
I think that where malmiteria gets it wrong is that he thinks that super is broken. Its not. But we might want to avoid MI, and use something like traits instead. Or composition.
Indeed. ChrisA