Timeline for The Liskov Substitution Principle, and Python
Current License: CC BY-SA 4.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 1, 2021 at 1:31 | comment | added | Kevin |
You only need to care about __init__ matching up if you're doing some weird cooperative multiple inheritance thing (in which case, you don't actually know what class will receive the super().__init__() call, so you have to pass everything through transparently with **kwargs or possibly *args , and then the interfaces had darned well better be compatible with each other).
|
|
Aug 31, 2021 at 15:41 | history | edited | Telastyn | CC BY-SA 4.0 |
Adjusted based on comments.
|
Aug 31, 2021 at 14:35 | comment | added | Caleth |
@AlexWaygood that seems like a strange restriction. It should probably call super().__init__() with appropriate params, but it's fine to hardcode or calculate those as appropriate.
|
|
Aug 31, 2021 at 14:23 | comment | added | Alex Waygood |
@Caleth: Would you argue that all classmethods should be seen as outside of the interface, or only ones like fromkeys that would never plausibly be called from an instance? Python is peculiar, in that classmethods can be called from instances as well as the class itself, leading to common patterns where classmethods are called from within instance-method implementations.
|
|
Aug 31, 2021 at 14:21 | comment | added | Alex Waygood |
Really interesting, thanks for the response. Some follow-up questions. I've definitely seen it argued lots of times on Stack Overflow (and elsewhere) that it's bad form for a subclass's __init__ method to take a different number of parameters to the superclass. Would you argue that's generally correct, and incorrect in this case because object is a special-case at the bottom of all inheritance chains? Or would you argue that it's just much more complex than that, i.e., whether it's okay to modify the signature of __init__ in a subclass will vary from case to case?
|
|
Aug 31, 2021 at 14:12 | comment | added | Telastyn | That’s a good point. Python is one of my worst languages, so there may be nuance here I’m missing. | |
Aug 31, 2021 at 14:09 | comment | added | Caleth |
Arguably classmethods are not part of the interface. You aren't calling fromKeys on an instance of dict (be it a Counter or not)
|
|
Aug 31, 2021 at 14:06 | history | answered | Telastyn | CC BY-SA 4.0 |