[Python-ideas] Keyword only argument on function call
Steven D'Aprano
steve at pearwood.info
Wed Sep 12 10:39:18 EDT 2018
On Wed, Sep 12, 2018 at 02:23:34PM +0100, Jonathan Fine wrote:
> Steve Barnes suggested adding __params__, as in
>
> > def a_method(self, cr, uid, ids, values, context=None):
> > ...
> > params = {k:v for k,v in __params__ if k in parent.a_method.keys()}
> > # Possibly add some additional entries here!
> > super(self, parent).a_method(**params)
In context, what Steve Barnes said was
If this [__params__] was accessible externally,
as fn.__defaults__ is [...]
https://mail.python.org/pipermail/python-ideas/2018-September/053322.html
Here is the behaviour of fn.__defaults__:
py> def fn(a=1, b=2, c=3):
... pass
...
py> fn.__defaults__
(1, 2, 3)
Notice that it is an externally acessible attribute of the function
object. If that's not what Steve Barnes meant, then I have no idea why
fn.__defaults__ is relevant or what he meant.
I'll confess that I couldn't work out what Steve's code snippet was
supposed to mean:
params = {k:v for k,v in __params__ if k in parent.a_method.keys()}
Does __params__ refer to the currently executing a_method, or the
superclass method being called later on in the line?
Why doesn't parent.a_method have parens?
Since parent.a_method probably isn't a dict, why are we calling keys()
on a method object?
The whole snippet was too hard for me to comprehend, so I went by the
plain meaning of the words he used to describe the desired semantics.
If __params__ is like fn.__defaults__, then that would require setting
fn.__params__ on each call.
Perhaps I'm reading too much into the "accessible externally" part,
since Steve's example doesn't seem to actually be accessing it
externally.
--
Steve
More information about the Python-ideas
mailing list