4 ms·
What is the usual pattern here? I imagine you have A.__init__(self) and B.__init__(self) in C.__init__(), right? i.e. child logic is handled in the child, not
by renewiltord 6y ago
What is the usual pattern here? I imagine you have A.__init__(self) and B.__init__(self) in C.__init__(), right?
i.e. child logic is handled in the child, not in the parent.
- mehrdadn 6y agoNo I don't (the code is as-is), but in fact if I did, those those would still suffer from basically the same problem even if I did (exercise for the reader). In modern Python you're supposed to use super().__init__() to let it handle this issue with multiple inheritance.
- renewiltord 6y agoNo no, I understand the problem you're talking about. It is a problem. I'm asking about the solution. Why wouldn't you handle it in the child instead of having siblings be superable? Essentially, why not do it like this: class A(): def __init__(self): self.x = 1 class B(): def __init__(self): self.y = 2 class C(A, B): def __init__(self): A.__init__(self) B.__init__(self) print(C().y)
- theg5prank 6y agoNow your class C can't necessarily be used in a multiple inheritance hierarchy with some other class D, and a class E that inherits from C and D, as C hardcodes what its parents are. Python uses the C3 MRO algorithm to traverse all relevant classes in your hierarchy when you religiously use the super builtin. https://www.python.org/download/releases/2.3/mro/ https://www.python.org/download/releases/2.3/mro/ But; multiple inheritance is a beast in any language due to its complexity and bringing it up in a discussion of "magic" that python exposes beginners to is specious. It's true that C++ doesn't have this problem, because it has no super keyword and you must explicitly delegate everything, but also introduces the problem that you can have multiple instances of a base class and so has added complexity, virtual inheritance, to solve THAT problem. And tons of languages have a problem where omitting a call to "super" is logically required is not statically diagnosable.
- renewiltord 6y agoOhhh, that makes sense, because any later child will need to know that A and B are C's parents! Okay, thanks!
- theg5prank 6y agoThat, and: I forgot to mention that not using the super builtin can also lead to unintentionally calling a method twice or more for the same base class in a diamond situation (note that with new-style classes, all multiple inheritance has at least one "diamond" class, often object, but people normally do not delegate to object for obvious reasons; you can imagine a base class Z in this example that A and B inherit from instead, if you wish). So, not using the super builtin can result in both delegating to a superclass' implementation too much, and not at all! This is a problem with multiple inheritance that Python actually solves; virtual inheritance doesn't touch it. Note that `object` being the root of the class hierarchy implies that you must have careful coordination between all classes in a multiple inheritance hierarchy to determine, for a particular method, when subclasses may/must delegate to super, what the arguments to the method are, and even what the argument names are in some cases! You can't just go glomming classes together and expect it to go well unless you know the classes either don't use the same method names or agree very carefully on what the delgatable interfaces are.
- renewiltord 6y agoOh, ah yes, that's good. With the A.__init(self) approach you'd overcall in a diamond-pattern. Okay, I'm convinced.
- mehrdadn 6y ago> It's true that C++ doesn't have this problem, because it has no super keyword and you must explicitly delegate everything It's emphatically not "having a super keyword" or not having it. That's a red herring. Visual C++ has __super and yet it doesn't suffer from this: it errors with "ambiguous call to overloaded function" when there are multiple candidates. There's a lot more I could say about this (frankly the issue I outlined just scratch the surface of a deeper problem in Python), but this is diverting the discussion. Nobody even said C++ is simple or somehow easier to learn than Python in the first place; I was just pointing out an insidious issue in Python, and frankly, it could've been done better without any need to emulate C++'s approach at all. (Exercise for the reader: suggest improvements.)
- stOneskull 6y agoi think in modern python, you leave out the (object) and just write class A:
- mehrdadn 6y agoYou can, but Python 2 is still the default on some systems so I thought I'd just put it and make people's lives easier.