keyword arguments before positional arguments- syntax inconsistency
consider the following function: def foo(a,b,c,x): pass The following calls are equivalent: foo(1,2,3, x=0) foo(*(1,2,3), x=0) However, since python allows keyword arguments before star-unpacking, you can also do: foo(x=0, *(1, 2, 3)) But removing the unpacking, would result in a syntax error: foo(x=0, 1, 2, 3) This is against the understanding of unpacking, is this intentional?
Ben's right regarding the facts. Here's my examples. Python 3.6.9 (default, Jul 17 2020, 12:50:27) >>> def foo(*argv, **kwargs): return argv, kwargs >>> foo(*'ab', x=1, y=2, *'cd', z=3) (('a', 'b', 'c', 'd'), {'x': 1, 'y': 2, 'z': 3}) Python 2.7.17 (default, Jul 20 2020, 15:37:01) >>> def foo(*argv, **kwargs): return argv, kwargs >>> foo(*'ab', x=1, y=2, *'cd', z=3) SyntaxError: invalid syntax Also, in Python 3.6.9 >>> foo(**{}, *()) is a SyntaxError, but >>> foo(x=1, *()) is not. I think the change happened as a result of PEP 448 -- Additional Unpacking Generalizations https://www.python.org/dev/peps/pep-0448/ It reads, in part,
Function calls may accept an unbounded number of * and ** unpackings. There will be no restriction of the order of positional arguments with relation to * unpackings nor any restriction of the order of keyword arguments with relation to ** unpackings.
Ben also asked: This is against the understanding of unpacking, is this intentional? I was surprised at the unpacking behaviour. My first thought was that Ben had made some mistake regarding the facts. So I made the check you see above. -- Jonathan
I've found a note about this from Guido back in 2000:
https://mail.python.org/archives/list/python-dev@python.org/thread/BIAEXJPSO...
Not sure if it went anywhere back then.
On Tue, Aug 25, 2020 at 4:27 PM Ben Avrahami
consider the following function: def foo(a,b,c,x): pass
The following calls are equivalent: foo(1,2,3, x=0) foo(*(1,2,3), x=0) However, since python allows keyword arguments before star-unpacking, you can also do: foo(x=0, *(1, 2, 3)) But removing the unpacking, would result in a syntax error: foo(x=0, 1, 2, 3) This is against the understanding of unpacking, is this intentional? _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/L5TYF3... Code of Conduct: http://python.org/psf/codeofconduct/
participants (3)
-
Bar Harel
-
Ben Avrahami
-
Jonathan Fine