[Python-ideas] A sorted version of **kwargs

Steven D'Aprano steve at pearwood.info
Thu Jan 20 11:28:05 CET 2011


Don Spaulding wrote:

> Obviously, OrderedDict's __init__ method (like all other functions) never
> gets a chance to see the kwargs dict in the order it was specified.  It's
> usually faked by accepting the sequence of (key, val) tuples, as above.  I
> personally think it would be nice to be able to ask the interpreter to keep
> track of the order of the arguments to my function, something like:
> 
>     def sweet_function_name(*args, **kwargs, ***an_odict_of_kwargs):
>         pass
> 
> I'm not married to the syntax.  What do you think about the idea?

I would be +0 on making **kwargs an ordered dict automatically, and -1 
on adding ***ordered_kwargs. Because kwargs is mostly used only for 
argument passing, and generally with only a small number of items, it 
probably doesn't matter too much if it's slightly slower than an 
unordered dict. But adding a third asterisk just makes it ugly, and it 
leaves open the question what happens when you try to mix **kw and 
***okw in the same function call, as you do above.


-- 
Steven




More information about the Python-ideas mailing list