Keyword for direct pass through of kwargs to super

Hi! ***Disclaimer: I am relatively new to Python*** I propose to add some mechanism that can automatically collect everything what would normally be collected by **kwargs in the __init__ method and directly pass it through to the super().__init__ call without being accessible in the __init__ itself. This way the autocompletion/docstring generation could safely ignore this argument which before would have showed up as **kwargs. I believe that this could greatly enhance the readability of automated documentation and especially the autocomplete of an IDE. I would like something like this to work: class A: @pass_through_kwargs # like this? Or maybe __init__(self, a_value, ***pass_through_kwargs)? def __init__(self, a_value): super().__init__() # <- realize that nothing is passed in here class B: def __init__(self, some_value): pass class MyClass(A, B): def __init__(self): super().__init__(a_value=1, some_value=2) Where the autocomplete of `A(` shows me just `A(a_value)`. The pass_through_kwargs can safely be ignored, since it is not "a real input" of A. It is automatically merged with the parameters of the `super(A, self).__init__()` call. How exactly this should be done would have to be discussed in more detail (I guess the actual values should probably override the passed through ones). With the current python version the code would have looked like class A: def __init__(self, a_value, **kwargs): super().__init__(**kwargs) But the user could have had the false impression that additional kwargs could be given when instantiating A. Obviously they are purely there for getting the init in the MRO working with multiple inheritance. One could also add something like pass_through_args as well but the usability of that is probably much more limited. Could this even work in theory? I guess it might be quite tricky since `super().__init__` doesn't even have to be called. If that would be the case: should it be called automatically if pass_through_kwargs were given after the init is done? By the way: Thank you all for this amazing language! Cheers, Michael

This just isn't true. You often do things with the kargs before passing them or other values to super. By using **kargs in the constructor and the call to `super`, you are indicating that the signature passes through, but there are many other things you could do instead. The docs *should* show that the derived method takes the same args as the inherited one it overrides. That is very useful information that belongs there. Best, -- Carl Smith carl.input@gmail.com On 26 May 2018 at 01:06, Michael Lohmann <mial.lohmann+pythonml@gmail.com> wrote:

On Sat, May 26, 2018 at 02:06:40AM +0200, Michael Lohmann wrote:
Why is this an advantage? Consider the following scenario: you are reading an unfamiliar code base and come across a class instantiation: obj = Aardvark(27, spam=3, eggs=5, cheese=True) So you look up help(Aardvark), and it tells you that the signature is Aardvark.__init__(self, foo) What the hell? If Aardvark.__init__ only takes a single argument (aside from self), why on earth isn't it an error to pass those keyword arguments? If you're a beginner, this is probably going to be a mind-melting WTF-I-don't-even moment. And even if you're *not* a beginner, it will probably cause a momentary sense of existential dread as, for a moment, everything you thought you understood about Python seems wrong. And then you remember that there's an obscure feature that tells the class to lie to you about what parameters it takes. It is bad enough when a method collects a bunch of parameters under **kwargs and I have to pour through the documentation for all its super classes to work out what they are. But at least the use of an explicit **kwargs gives me a clue that this is what is going on. Making **kwargs implicit and invisible reminds me of the old saying about coding for maintenance. To paraphrase: Design language features as if your language's users -- all of them -- are violent psychopaths who know where you live. [...]
But they *can* be given when instantiating A. What makes you think they can't be?
Obviously they are purely there for getting the init in the MRO working with multiple inheritance.
That's not always true. Even if it were true, it isn't obviously true. -- Steve

This just isn't true. You often do things with the kargs before passing them or other values to super. By using **kargs in the constructor and the call to `super`, you are indicating that the signature passes through, but there are many other things you could do instead. The docs *should* show that the derived method takes the same args as the inherited one it overrides. That is very useful information that belongs there. Best, -- Carl Smith carl.input@gmail.com On 26 May 2018 at 01:06, Michael Lohmann <mial.lohmann+pythonml@gmail.com> wrote:

On Sat, May 26, 2018 at 02:06:40AM +0200, Michael Lohmann wrote:
Why is this an advantage? Consider the following scenario: you are reading an unfamiliar code base and come across a class instantiation: obj = Aardvark(27, spam=3, eggs=5, cheese=True) So you look up help(Aardvark), and it tells you that the signature is Aardvark.__init__(self, foo) What the hell? If Aardvark.__init__ only takes a single argument (aside from self), why on earth isn't it an error to pass those keyword arguments? If you're a beginner, this is probably going to be a mind-melting WTF-I-don't-even moment. And even if you're *not* a beginner, it will probably cause a momentary sense of existential dread as, for a moment, everything you thought you understood about Python seems wrong. And then you remember that there's an obscure feature that tells the class to lie to you about what parameters it takes. It is bad enough when a method collects a bunch of parameters under **kwargs and I have to pour through the documentation for all its super classes to work out what they are. But at least the use of an explicit **kwargs gives me a clue that this is what is going on. Making **kwargs implicit and invisible reminds me of the old saying about coding for maintenance. To paraphrase: Design language features as if your language's users -- all of them -- are violent psychopaths who know where you live. [...]
But they *can* be given when instantiating A. What makes you think they can't be?
Obviously they are purely there for getting the init in the MRO working with multiple inheritance.
That's not always true. Even if it were true, it isn't obviously true. -- Steve
participants (3)
-
Carl Smith
-
Michael Lohmann
-
Steven D'Aprano