[Python-ideas] Keyword only argument on function call

David Mertz mertz at gnosis.cx
Sat Sep 8 19:23:11 EDT 2018


>
> def foo():
>     a, b, c = 1, 2, 3
>     function(a=77, **use('b d'))
>
> foo()
> You get the output “77 None 33 None”. So basically it doesn’t work at all.
> For the reason I wrote clearly in my last mail. You have to dig through the
> stack frames to make use() work.
>

OK, you are right.  Improved implementation.  Still not very hard.

In any case, I'm concerned with the API to *use* the `use()` function, not
how it's implemented.  The point is really just that we can accomplish the
same thing you want without syntax added.

>>> import inspect
>>> def reach(name):
...     for f in inspect.stack():
...         if name in f[0].f_locals:
...             return f[0].f_locals[name]
...     return None
...
>>> def use(names):
...     kws = {}
...     for name in names.split():
...         kws[name] = reach(name)
...     return kws
...
>>> def function(a=11, b=22, c=33, d=44):
...     print(a, b, c, d)
...
>>> function(a=77, **use('b d'))
77 None 33 None
>>> def foo():
...     a, b, c = 1, 2, 3
...     function(a=77, **use('b d'))
...
>>> foo()
77 2 33 None
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180908/9b9b6edd/attachment.html>


More information about the Python-ideas mailing list