[Python-ideas] On the Python function call interface

Chris Angelico rosuav at gmail.com
Tue Sep 11 07:58:18 EDT 2018


On Tue, Sep 11, 2018 at 8:03 PM, Jonathan Fine <jfine2358 at gmail.com> wrote:
> In some case, the implementer might prefer
>
>     def method(self, aaa, bbb, **kwargs):
>
>         # Do something with aaa and bbb.
>         super().method(**kwargs)
>
>
> However, the CALLER might wish that the implementer has a signature
>
>     def method(self, aaa, bbb, ccc, ddd, eee, fff, ggg, hhh):
>
> and this encourages the implementer to write super().method(aaa=aaa, ...).
>
>
> However, there is an alternative:
>
>     def method(self, aaa, bbb, ccc, ddd, eee, fff, ggg, hhh):
>
>         lcls = dict(locals())
>         lcls.pop('aaa')
>         lcls.pop('bbb')
>
>         # Do something with aaa and bbb.
>         super().method(**lcls)

What about this alternative:

@override
def method(self, aaa, bbb, **kwargs):
    # ... stuff with aaa and bbb
    super().method(**kwargs)

The decorator would do something akin to functools.wraps(), but
assuming that it's the class's immediate parent (not sure how you'd
determine that at function definition time, but let's assume that can
be done), and intelligently handling the added args. Basically, it
would replace kwargs with every legal keyword argument of the
corresponding method on the class's immediate parent. Yes, I know that
super() can pass a function call sideways, but if that happens, the
only flaw is in the signature, not the implementation (ie tab
completion might fail but the code still runs).

ChrisA


More information about the Python-ideas mailing list