I am studying Python3 tutorial on keyword arguments and couldn't reproduce the output due to the following code:
def cheeseshop(kind, *arguments, **keywords): print("-- Do you have any", kind, "?") print("-- I'm sorry, we're all out of", kind) for arg in arguments: print(arg) print("-" * 40) for kw in keywords: print(kw, ":", keywords[kw]) cheeseshop("Limburger", "It's very runny, sir.", "It's really very, VERY runny, sir.", shopkeeper="Michael Palin", client="John Cleese", sketch="Cheese Shop Sketch") -- Do you have any Limburger ? -- I'm sorry, we're all out of Limburger It's very runny, sir. It's really very, VERY runny, sir. ---------------------------------------- shopkeeper : Michael Palin client : John Cleese sketch : Cheese Shop Sketch
What I got was a sorted dict:
---------------------------------------- client : John Cleese shopkeeper : Michael Palin sketch : Cheese Shop Sketch
So I tried without calling cheeseshop():
>>> kw = {'shopkeeper':"Michael Palin", 'client':"John Cleese", 'sketch':"Cheese Shop Sketch"} >>> kw {'client': 'John Cleese', 'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch'}
It looks like in version 3.5, the keys are automatically sorted. But in version 2.7, they aren't:
>>> kw {'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch', 'client': 'John Cleese'}
and I have to sort it in 2.7 to agree with 3.5.
>>> for k in sorted(kw): ... print(k + " : " + kw[k]) ... client : John Cleese shopkeeper : Michael Palin sketch : Cheese Shop Sketch
So the statement in the tutorial: "Note that the order in which the keyword arguments are printed is guaranteed to match the order in which they were provided in the function call." should be only applied to version 2.7, not 3.5. Is this true?