[Python-ideas] Keyword only argument on function call

David Mertz mertz at gnosis.cx
Sat Sep 8 10:14:40 EDT 2018


I'm not sure whether my toy function is better to assume None for a name
that is "used" but does not exist, or to raise a NameError.  I can see
arguments in both directions, but either behavior is a very small number of
lines (and the same decision exists for the proposed syntax).  You might
also allow the `use()` function to take some argument(s) other than a
space-separated string, but that's futzing with a demonstration API.

On Sat, Sep 8, 2018 at 10:05 AM David Mertz <mertz at gnosis.cx> wrote:

> On Sat, Sep 8, 2018 at 9:34 AM Anders Hovmöller <boxed at killingar.net>
> wrote:
>
>>     function(a=my_a, c=my_c, *, b, d)
>>     function(*, b, c, d, a=my_a, c=my_c)
>>
>
> Yes, those look less bad.  They are also almost certainly should get this
> message rather than working:
>
>     TypeError: function() got multiple values for keyword argument 'c'
>
> But they also force changing the order of keyword arguments in the call.
> That doesn't do anything to the *behavior* of the call, but it often
> affects readability.
>
> For functions with lots of keyword arguments there is often a certain
> convention about the order they are passed in that readers expect to see.
> Those examples of opening and reading files that several people have given
> are good examples of this.  I.e. most optional arguments are not used, but
> when they are used they have certain relationships among them that lead
> readers to expect them in a certain order.
>
> Here's a counter-proposal that does not require any new syntax.  Is there
> ANYTHING your new syntax would really get you that this solution does not
> accomplish?! (other than save 4 characters; fewer if you came of with a one
> character name for the helper)
>
> >>> def function(a=11, b=22, c=33, d=44):
> ...     print(a, b, c, d)
> ...
> >>> a, b, c = 1, 2, 3
> >>> function(a=77, **use('b d'))
> 77 2 33 None
>
>
> We could implement this helper function like this:
>
> >>> def use(names):
> ...     kws = {}
> ...     for name in names.split():
> ...         try:
> ...             val = eval(name)
> ...         except:
> ...             val = None
> ...         kws[name] = val
> ...     return kws
>
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180908/a2155e03/attachment-0001.html>


More information about the Python-ideas mailing list