[Python-ideas] Preserving **kwargs order (was: Re: OrderedDict literals)

Chris Angelico rosuav at gmail.com
Thu Mar 20 01:13:28 CET 2014


On Thu, Mar 20, 2014 at 10:32 AM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> 1. Change kwargs to an OrderedDict for functions decorated a special decorator:
>
>   @preserve_kwargs_order
>   def spam(a, b, **kwargs):
>      ...  # kwargs will be an OrderedDict
>
> 2. Store the ordered keys in a list and bind that to a special local
> variable, but only for functions that have **kwargs:
>
>   def spam(a, b, **kwargs):
>       kwargs = OrderedDict((k, kwargs[k]) for k in __kwargs_order__)
>       ...
>
> My gut feeling is that option 2 is the better choice.

Strongly against any proposal that makes argument passing behave
differently based on the target - it means the process of building up
the arguments has to check some attribute on the function.

Order of kwargs is a problem for any function that passes args through
to another function.

def wrap_spam(*args, **kwargs):
    try:
        return spam(*args, **kwargs)
    except EggsError:
        return False

Does wrap_spam have to be aware that spam cares about keyword argument
order? Should it preserve order "just in case"? Should the
__kwargs_order__ list be automatically propagated? And what happens if
the args aren't passed through perfectly, but some are added/removed?

ChrisA


More information about the Python-ideas mailing list