[Python-Dev] Order of positional and keyword arguments
Chris Jerdonek
chris.jerdonek at gmail.com
Fri Apr 27 05:46:27 EDT 2018
On Thu, Apr 26, 2018 at 12:25 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> f(b=2, *[1]) is surprised in two ways:
>
> 1. Argument values are passed not in order. The first value is assigned to
> the second parameter, and the second value is assigned to the first
> parameter.
>
> 2. Argument values are evaluated not from left to right. This contradicts
> the general rule that expressions are evaluated from left to right (with few
> known exceptions).
>
> I never seen the form `f(b=2, *[1])` in practice (though the language
> reference contains an explicit example for it), and it looks weird to me. I
> don't see reasons of writing `f(b=2, *[1])` instead of more natural `f(*[1],
> b=2)`. I propose to disallow it.
Coincidentally, I recently came across and reviewed a PR to Django
that proposed exactly this, or at least something very similar. They
proposed changing--
def create_cursor(self, name=None):
to--
def create_cursor(self, name=None, *args, **kwargs):
https://github.com/django/django/pull/9674/files#diff-53fcf3ac0535307033e0cfabb85c5301R173
--Chris
>
> This will also make the grammar simpler. Current grammar:
>
> argument_list: `positional_arguments` ["," `starred_and_keywords`]
> : ["," `keywords_arguments`]
> : | `starred_and_keywords` ["," `keywords_arguments`]
> : | `keywords_arguments`
> positional_arguments: ["*"] `expression` ("," ["*"] `expression`)*
> starred_and_keywords: ("*" `expression` | `keyword_item`)
> : ("," "*" `expression` | "," `keyword_item`)*
> keywords_arguments: (`keyword_item` | "**" `expression`)
> : ("," `keyword_item` | "," "**" `expression`)*
> keyword_item: `identifier` "=" `expression`
>
> Proposed grammar:
>
> argument_list: `positional_arguments` ["," `keywords_arguments`]
> : | `keywords_arguments`
> positional_arguments: ["*"] `expression` ("," ["*"] `expression`)*
> keywords_arguments: `keyword_argument` ("," `keyword_argument`)*
> keyword_argument: `identifier` "=" `expression` | "**" `expression`
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/chris.jerdonek%40gmail.com
More information about the Python-Dev
mailing list