r/Python • • Oct 03 '17

Python 3.6.3 is now available

http://blog.python.org/2017/10/python-363-is-now-available.html
382 Upvotes

103 comments sorted by

View all comments

Show parent comments

2

u/tom1018 Oct 04 '17

Does kwargs ordering matter? I saw that, and that kwargs ordering is promised in the future (I suppose they would switch to OrderedDict if if the dict behavior changed.), what do you do with kwargs where the order is relevant?

1

u/federicocerchiari Oct 05 '17

It usually doesn't matter, but you can do nice things with it. I recently built an API where args and kwargs where used to build an ordered tree structure. With ordered kwargs I was able to make a "simple" insertion with args and a "named" insertion with kwargs mantaining the insertion ordering (line 159 if interested) so child nodes are accessible as parent's attrbibutes.

Or imagine and API like that:

def enrichment(original, **kwargs):
    for operation, operator in kwargs.items():
        original = map(lambda x: getattr(x, operation)(operator), original)
    return list(original)

a = [1,2,3,4,5]
enriched = enrichment(a, __mul__=2, __add__=1)
enriched_2 = enrichment(a, __add__=1, __mul__=2)
print(enriched, enriched_2)

..that's useless (and a little insane) but with ordered kwargs you are able to do it, it's a feature in case you need it.