DIP 1030--Named Arguments--Community Review Round 1 Discussion

jmh530 john.michael.hall at gmail.com
Thu Feb 20 11:59:13 UTC 2020


On Thursday, 20 February 2020 at 11:00:16 UTC, Atila Neves wrote:
> [snip]
>
> Nope: https://www.python.org/dev/peps/pep-0570/

1) Pep 570 is opt-in*. It gives you a bit more control so you can 
have positional-only and key-word only arguments. However, if you 
do not opt-in, then python still has a mixed of positional and 
named arguments for everything. See the standard_arg function in 
function examples:

https://www.python.org/dev/peps/pep-0570/#function-examples

In particular, I was thinking of situation where you have two 
functions

def foo(arg1, arg2):
def bar(arg1 other1):

The following code is completely valid, both pre and post pep 570

foo(a, b)
bar(a, b)
foo(arg2=b, arg1=a)
bar(other1=b, arg1=a)

In my case, I was noticing that other1 is a lot like arg2, so you 
want to change other1's name to arg2. However, that will break 
the last line. Under pep 570, you could have instead written

def foo(arg1, arg2, /):
def bar(arg1 other1, /):

and it won't break when you re-name it, but then you can't use 
the last two lines. Alternately, if try to use something like

def foo(*, arg1, arg2):
def bar(*, arg1 other1):

then the first two lines calling foo and bar won't work. So even 
under pep 570, you would still be breaking code to re-name it. 
The problem comes from the standard method of mixing positional 
or keyword arguments.

2) Referring again to the pep 570 function examples, they have

def combined_example(pos_only, /, mixed, *, kwd_only):

where mixed (called standard in their example) is the mix of 
positional and keyword  arguments. If D were to move in this 
direction, then we would have a similar function like

void combined_example(Type1 pos_only, /, Type2 mixed, *, Type3 
kwd_only)

The only thing I would change is that pos_only should remain the 
default. So that would imply that the python function examples 
would instead be something like below in D.

void standard_arg(Type1 pos_only)
void mixed_arg(/, Type2 mixed)
void kwd_only_arg(*, Type3 kwd_only)

This would make named arguments opt-in and reduce the risk of 
people causing code breakage by changing APIs. One could start 
with just the mixed and consider adding the keyword only approach 
in the future if people want/need it.


* That pep only was implemented in Python 3.8, which only came 
out in October of last year, I think I'm using 3.7 at work...so 
admittedly less familiar with it.



More information about the Digitalmars-d mailing list