In python, I wrote this function to teach myself how **kwargs works in Python:
def fxn(a1, **kwargs):
print a1
for k in kwargs:
This is a dictionary. And, as mentioned in documentation, dictionary has no order (from http://docs.python.org/tutorial/datastructures.html#dictionaries):
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
But you can make it processed in some order, like that:
using sorted():
def fxn(a1, **kwargs):
print a1
for k in sorted(kwargs): # notice "kwargs" replaced by "sorted(kwargs)"
print k, " : ", kwargs[k]
or by using OrderedDict type (you can pass OrderedDict object as parameter containing all the key-value pairs):
from collections import OrderedDict
def fxn(a1, ordkwargs):
print a1
for k in ordkwargs:
print k, " : ", ordkwargs[k]
fxn(3, OrderedDict((('a2',2), ('a3',3), ('a4',4))))