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

Chris Angelico rosuav at gmail.com
Tue Apr 15 10:09:49 CEST 2014


On Tue, Apr 15, 2014 at 2:12 PM, Franklin? Lee
<leewangzhong+python at gmail.com> wrote:
> ... A-and on the flip side, I put forth that maybe Guido shouldn't be doing
> that kind of thing in the first place (*gasp*)! If he wanted a copy, he
> should've explicitly copied it with dict(), instead of relying on behavior
> that I've never even seen a doc reference for! (Not sure if I actually
> believe that **kwargs should be some subclass of FrozenDict, but maybe
> someone else does, and they can convince us.)

A function's collected kwargs will always be a new dictionary. That's
a language guarantee. There are a number of common idioms that make
use of this, such as subclassing and consuming only certain args:

    def __init__(self, *, **kwargs):
        self.foo = kwargs.pop("foo")
        super().__init__(**kwargs)

ChrisA


More information about the Python-ideas mailing list